Chapter 9. Another Look at Variables

Used properly, variables can add power and flexibility to scripts. This requires learning their subtleties and nuances.

9.1. Internal Variables

Builtin variables:

variables affecting bash script behavior

$BASH

The path to the Bash binary itself
 bash$ echo $BASH
 /bin/bash

$BASH_ENV

An environmental variable pointing to a Bash startup file to be read when a script is invoked

$BASH_SUBSHELL

A variable indicating the subshell level. This is a new addition to Bash, version 3.

See Example 21-1 for usage.

$BASHPID

Process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result.

 bash4$ echo $$
 11015
 
 
 bash4$ echo $BASHPID
 11015
 
 
 bash4$ ps ax | grep bash4
 11015 pts/2    R      0:00 bash4
 	      

But ...

   1 #!/bin/bash4
   2 
   3 echo "\$\$ outside of subshell = $$"                              # 9602
   4 echo "\$BASH_SUBSHELL  outside of subshell = $BASH_SUBSHELL"      # 0
   5 echo "\$BASHPID outside of subshell = $BASHPID"                   # 9602
   6 
   7 echo
   8 
   9 ( echo "\$\$ inside of subshell = $$"                             # 9602
  10   echo "\$BASH_SUBSHELL inside of subshell = $BASH_SUBSHELL"      # 1
  11   echo "\$BASHPID inside of subshell = $BASHPID" )                # 9603
  12   # Note that $$ returns PID of parent process.

$BASH_VERSINFO[n]

A 6-element array containing version information about the installed release of Bash. This is similar to $BASH_VERSION, below, but a bit more detailed.

   1 # Bash version info:
   2 
   3 for n in 0 1 2 3 4 5
   4 do
   5   echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
   6 done  
   7 
   8 # BASH_VERSINFO[0] = 3                      # Major version no.
   9 # BASH_VERSINFO[1] = 00                     # Minor version no.
  10 # BASH_VERSINFO[2] = 14                     # Patch level.
  11 # BASH_VERSINFO[3] = 1                      # Build version.
  12 # BASH_VERSINFO[4] = release                # Release status.
  13 # BASH_VERSINFO[5] = i386-redhat-linux-gnu  # Architecture
  14                                             # (same as $MACHTYPE).

$BASH_VERSION

The version of Bash installed on the system

 bash$ echo $BASH_VERSION
 3.2.25(1)-release
 	      

 tcsh% echo $BASH_VERSION
 BASH_VERSION: Undefined variable.
 	      

Checking $BASH_VERSION is a good method of determining which shell is running. $SHELL does not necessarily give the correct answer.

$CDPATH

A colon-separated list of search paths available to the cd command, similar in function to the $PATH variable for binaries. The $CDPATH variable may be set in the local ~/.bashrc file.

 bash$ cd bash-doc
 bash: cd: bash-doc: No such file or directory
 
 
 bash$ CDPATH=/usr/share/doc
 bash$ cd bash-doc
 /usr/share/doc/bash-doc
 
 
 bash$ echo $PWD
 /usr/share/doc/bash-doc
 	      

$DIRSTACK

The top value in the directory stack [1] (affected by pushd and popd)

This builtin variable corresponds to the dirs command, however dirs shows the entire contents of the directory stack.

$EDITOR

The default editor invoked by a script, usually vi or emacs.

$EUID

"effective" user ID number

Identification number of whatever identity the current user has assumed, perhaps by means of su.

Caution

The $EUID is not necessarily the same as the $UID.

$FUNCNAME

Name of the current function

   1 xyz23 ()
   2 {
   3   echo "$FUNCNAME now executing."  # xyz23 now executing.
   4 }
   5 
   6 xyz23
   7 
   8 echo "FUNCNAME = $FUNCNAME"        # FUNCNAME =
   9                                    # Null value outside a function.

See also Example A-50.

$GLOBIGNORE

A list of filename patterns to be excluded from matching in globbing.

$GROUPS

Groups current user belongs to

This is a listing (array) of the group id numbers for current user, as recorded in /etc/passwd and /etc/group.

 root# echo $GROUPS
 0
 
 
 root# echo ${GROUPS[1]}
 1
 
 
 root# echo ${GROUPS[5]}
 6
 	      

$HOME

Home directory of the user, usually /home/username (see Example 10-7)

$HOSTNAME

The hostname command assigns the system host name at bootup in an init script. However, the gethostname() function sets the Bash internal variable $HOSTNAME. See also Example 10-7.

$HOSTTYPE

host type

Like $MACHTYPE, identifies the system hardware.

 bash$ echo $HOSTTYPE
 i686
$IFS

internal field separator

This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

$IFS defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse a comma-separated data file. Note that $* uses the first character held in $IFS. See Example 5-1.

 bash$ echo "$IFS"
 
 (With $IFS set to default, a blank line displays.)
 	      
 
 
 bash$ echo "$IFS" | cat -vte
  ^I$
 $
 (Show whitespace: here a single space, ^I [horizontal tab],
  and newline, and display "$" at end-of-line.)
 
 
 
 bash$ bash -c 'set w x y z; IFS=":-;"; echo "$*"'
 w:x:y:z
 (Read commands from string and assign any arguments to pos params.)
 	      

Set $IFS to eliminate whitespace in pathnames.
   1 IFS="$(printf '\n\t')"   # Per David Wheeler.

Caution

$IFS does not handle whitespace the same as it does other characters.


Example 9-1. $IFS and whitespace

   1 #!/bin/bash
   2 # ifs.sh
   3 
   4 
   5 var1="a+b+c"
   6 var2="d-e-f"
   7 var3="g,h,i"
   8 
   9 IFS=+
  10 # The plus sign will be interpreted as a separator.
  11 echo $var1     # a b c
  12 echo $var2     # d-e-f
  13 echo $var3     # g,h,i
  14 
  15 echo
  16 
  17 IFS="-"
  18 # The plus sign reverts to default interpretation.
  19 # The minus sign will be interpreted as a separator.
  20 echo $var1     # a+b+c
  21 echo $var2     # d e f
  22 echo $var3     # g,h,i
  23 
  24 echo
  25 
  26 IFS=","
  27 # The comma will be interpreted as a separator.
  28 # The minus sign reverts to default interpretation.
  29 echo $var1     # a+b+c
  30 echo $var2     # d-e-f
  31 echo $var3     # g h i
  32 
  33 echo
  34 
  35 IFS=" "
  36 # The space character will be interpreted as a separator.
  37 # The comma reverts to default interpretation.
  38 echo $var1     # a+b+c
  39 echo $var2     # d-e-f
  40 echo $var3     # g,h,i
  41 
  42 # ======================================================== #
  43 
  44 # However ...
  45 # $IFS treats whitespace differently than other characters.
  46 
  47 output_args_one_per_line()
  48 {
  49   for arg
  50   do
  51     echo "[$arg]"
  52   done #  ^    ^   Embed within brackets, for your viewing pleasure.
  53 }
  54 
  55 echo; echo "IFS=\" \""
  56 echo "-------"
  57 
  58 IFS=" "
  59 var=" a  b c   "
  60 #    ^ ^^   ^^^
  61 output_args_one_per_line $var  # output_args_one_per_line `echo " a  b c   "`
  62 # [a]
  63 # [b]
  64 # [c]
  65 
  66 
  67 echo; echo "IFS=:"
  68 echo "-----"
  69 
  70 IFS=:
  71 var=":a::b:c:::"               # Same pattern as above,
  72 #    ^ ^^   ^^^                #+ but substituting ":" for " "  ...
  73 output_args_one_per_line $var
  74 # []
  75 # [a]
  76 # []
  77 # [b]
  78 # [c]
  79 # []
  80 # []
  81 
  82 # Note "empty" brackets.
  83 # The same thing happens with the "FS" field separator in awk.
  84 
  85 
  86 echo
  87 
  88 exit

(Many thanks, Stéphane Chazelas, for clarification and above examples.)

See also Example 16-41, Example 11-8, and Example 19-14 for instructive examples of using $IFS.

$IGNOREEOF

Ignore EOF: how many end-of-files (control-D) the shell will ignore before logging out.

$LC_COLLATE

Often set in the .bashrc or /etc/profile files, this variable controls collation order in filename expansion and pattern matching. If mishandled, LC_COLLATE can cause unexpected results in filename globbing.

Note

As of version 2.05 of Bash, filename globbing no longer distinguishes between lowercase and uppercase letters in a character range between brackets. For example, ls [A-M]* would match both File1.txt and file1.txt. To revert to the customary behavior of bracket matching, set LC_COLLATE to C by an export LC_COLLATE=C in /etc/profile and/or ~/.bashrc.

$LC_CTYPE

This internal variable controls character interpretation in globbing and pattern matching.

$LINENO

This variable is the line number of the shell script in which this variable appears. It has significance only within the script in which it appears, and is chiefly useful for debugging purposes.

   1 # *** BEGIN DEBUG BLOCK ***
   2 last_cmd_arg=$_  # Save it.
   3 
   4 echo "At line number $LINENO, variable \"v1\" = $v1"
   5 echo "Last command argument processed = $last_cmd_arg"
   6 # *** END DEBUG BLOCK ***

$MACHTYPE

machine type

Identifies the system hardware.

 bash$ echo $MACHTYPE
 i686
$OLDPWD

Old working directory ("OLD-Print-Working-Directory", previous directory you were in).

$OSTYPE

operating system type

 bash$ echo $OSTYPE
 linux
$PATH

Path to binaries, usually /usr/bin/, /usr/X11R6/bin/, /usr/local/bin, etc.

When given a command, the shell automatically does a hash table search on the directories listed in the path for the executable. The path is stored in the environmental variable, $PATH, a list of directories, separated by colons. Normally, the system stores the $PATH definition in /etc/profile and/or ~/.bashrc (see Appendix H).

 bash$ echo $PATH
 /bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin

PATH=${PATH}:/opt/bin appends the /opt/bin directory to the current path. In a script, it may be expedient to temporarily add a directory to the path in this way. When the script exits, this restores the original $PATH (a child process, such as a script, may not change the environment of the parent process, the shell).

Note

The current "working directory", ./, is usually omitted from the $PATH as a security measure.

$PIPESTATUS

Array variable holding exit status(es) of last executed foreground pipe.

 bash$ echo $PIPESTATUS
 0
 
 bash$ ls -al | bogus_command
 bash: bogus_command: command not found
 bash$ echo ${PIPESTATUS[1]}
 127
 
 bash$ ls -al | bogus_command
 bash: bogus_command: command not found
 bash$ echo $?
 127
 	      

The members of the $PIPESTATUS array hold the exit status of each respective command executed in a pipe. $PIPESTATUS[0] holds the exit status of the first command in the pipe, $PIPESTATUS[1] the exit status of the second command, and so on.

Caution

The $PIPESTATUS variable may contain an erroneous 0 value in a login shell (in releases prior to 3.0 of Bash).

 tcsh% bash
 
 bash$ who | grep nobody | sort
 bash$ echo ${PIPESTATUS[*]}
 0
 	      

The above lines contained in a script would produce the expected 0 1 0 output.

Thank you, Wayne Pollock for pointing this out and supplying the above example.

Note

The $PIPESTATUS variable gives unexpected results in some contexts.

 bash$ echo $BASH_VERSION
 3.00.14(1)-release
 
 bash$ $ ls | bogus_command | wc
 bash: bogus_command: command not found
 0       0       0
 
 bash$ echo ${PIPESTATUS[@]}
 141 127 0
 	      

Chet Ramey attributes the above output to the behavior of ls. If ls writes to a pipe whose output is not read, then SIGPIPE kills it, and its exit status is 141. Otherwise its exit status is 0, as expected. This likewise is the case for tr.

Note

$PIPESTATUS is a "volatile" variable. It needs to be captured immediately after the pipe in question, before any other command intervenes.

 bash$ $ ls | bogus_command | wc
 bash: bogus_command: command not found
 0       0       0
 
 bash$ echo ${PIPESTATUS[@]}
 0 127 0
 
 bash$ echo ${PIPESTATUS[@]}
 0
 	      

Note

The pipefail option may be useful in cases where $PIPESTATUS does not give the desired information.

$PPID

The $PPID of a process is the process ID (pid) of its parent process. [2]

Compare this with the pidof command.

$PROMPT_COMMAND

A variable holding a command to be executed just before the primary prompt, $PS1 is to be displayed.

$PS1

This is the main prompt, seen at the command-line.

$PS2

The secondary prompt, seen when additional input is expected. It displays as ">".

$PS3

The tertiary prompt, displayed in a select loop (see Example 11-30).

$PS4

The quartenary prompt, shown at the beginning of each line of output when invoking a script with the -x [verbose trace] option. It displays as "+".

As a debugging aid, it may be useful to embed diagnostic information in $PS4.
   1 P4='$(read time junk < /proc/$$/schedstat; echo "@@@ $time @@@ " )'
   2 # Per suggestion by Erik Brandsberg.
   3 set -x
   4 # Various commands follow ...

$PWD

Working directory (directory you are in at the time)

This is the analog to the pwd builtin command.

   1 #!/bin/bash
   2 
   3 E_WRONG_DIRECTORY=85
   4 
   5 clear # Clear the screen.
   6 
   7 TargetDirectory=/home/bozo/projects/GreatAmericanNovel
   8 
   9 cd $TargetDirectory
  10 echo "Deleting stale files in $TargetDirectory."
  11 
  12 if [ "$PWD" != "$TargetDirectory" ]
  13 then    # Keep from wiping out wrong directory by accident.
  14   echo "Wrong directory!"
  15   echo "In $PWD, rather than $TargetDirectory!"
  16   echo "Bailing out!"
  17   exit $E_WRONG_DIRECTORY
  18 fi  
  19 
  20 rm -rf *
  21 rm .[A-Za-z0-9]*    # Delete dotfiles.
  22 # rm -f .[^.]* ..?*   to remove filenames beginning with multiple dots.
  23 # (shopt -s dotglob; rm -f *)   will also work.
  24 # Thanks, S.C. for pointing this out.
  25 
  26 #  A filename (`basename`) may contain all characters in the 0 - 255 range,
  27 #+ except "/".
  28 #  Deleting files beginning with weird characters, such as -
  29 #+ is left as an exercise. (Hint: rm ./-weirdname or rm -- -weirdname)
  30 result=$?   # Result of delete operations. If successful = 0.
  31 
  32 echo
  33 ls -al              # Any files left?
  34 echo "Done."
  35 echo "Old files deleted in $TargetDirectory."
  36 echo
  37 
  38 # Various other operations here, as necessary.
  39 
  40 exit $result

$REPLY

The default value when a variable is not supplied to read. Also applicable to select menus, but only supplies the item number of the variable chosen, not the value of the variable itself.

   1 #!/bin/bash
   2 # reply.sh
   3 
   4 # REPLY is the default value for a 'read' command.
   5 
   6 echo
   7 echo -n "What is your favorite vegetable? "
   8 read
   9 
  10 echo "Your favorite vegetable is $REPLY."
  11 #  REPLY holds the value of last "read" if and only if
  12 #+ no variable supplied.
  13 
  14 echo
  15 echo -n "What is your favorite fruit? "
  16 read fruit
  17 echo "Your favorite fruit is $fruit."
  18 echo "but..."
  19 echo "Value of \$REPLY is still $REPLY."
  20 #  $REPLY is still set to its previous value because
  21 #+ the variable $fruit absorbed the new "read" value.
  22 
  23 echo
  24 
  25 exit 0

$SECONDS

The number of seconds the script has been running.

   1 #!/bin/bash
   2 
   3 TIME_LIMIT=10
   4 INTERVAL=1
   5 
   6 echo
   7 echo "Hit Control-C to exit before $TIME_LIMIT seconds."
   8 echo
   9 
  10 while [ "$SECONDS" -le "$TIME_LIMIT" ]
  11 do   #   $SECONDS is an internal shell variable.
  12   if [ "$SECONDS" -eq 1 ]
  13   then
  14     units=second
  15   else  
  16     units=seconds
  17   fi
  18 
  19   echo "This script has been running $SECONDS $units."
  20   #  On a slow or overburdened machine, the script may skip a count
  21   #+ every once in a while.
  22   sleep $INTERVAL
  23 done
  24 
  25 echo -e "\a"  # Beep!
  26 
  27 exit 0

$SHELLOPTS

The list of enabled shell options, a readonly variable.
 bash$ echo $SHELLOPTS
 braceexpand:hashall:histexpand:monitor:history:interactive-comments:emacs
 	      

$SHLVL

Shell level, how deeply Bash is nested. [3] If, at the command-line, $SHLVL is 1, then in a script it will increment to 2.

Note

This variable is not affected by subshells. Use $BASH_SUBSHELL when you need an indication of subshell nesting.

$TMOUT

If the $TMOUT environmental variable is set to a non-zero value time, then the shell prompt will time out after $time seconds. This will cause a logout.

As of version 2.05b of Bash, it is now possible to use $TMOUT in a script in combination with read.

   1 # Works in scripts for Bash, versions 2.05b and later.
   2 
   3 TMOUT=3    # Prompt times out at three seconds.
   4 
   5 echo "What is your favorite song?"
   6 echo "Quickly now, you only have $TMOUT seconds to answer!"
   7 read song
   8 
   9 if [ -z "$song" ]
  10 then
  11   song="(no answer)"
  12   # Default response.
  13 fi
  14 
  15 echo "Your favorite song is $song."

There are other, more complex, ways of implementing timed input in a script. One alternative is to set up a timing loop to signal the script when it times out. This also requires a signal handling routine to trap (see Example 32-5) the interrupt generated by the timing loop (whew!).


Example 9-2. Timed Input

   1 #!/bin/bash
   2 # timed-input.sh
   3 
   4 # TMOUT=3    Also works, as of newer versions of Bash.
   5 
   6 TIMER_INTERRUPT=14
   7 TIMELIMIT=3  # Three seconds in this instance.
   8              # May be set to different value.
   9 
  10 PrintAnswer()
  11 {
  12   if [ "$answer" = TIMEOUT ]
  13   then
  14     echo $answer
  15   else       # Don't want to mix up the two instances. 
  16     echo "Your favorite veggie is $answer"
  17     kill $!  #  Kills no-longer-needed TimerOn function
  18              #+ running in background.
  19              #  $! is PID of last job running in background.
  20   fi
  21 
  22 }  
  23 
  24 
  25 TimerOn()
  26 {
  27   sleep $TIMELIMIT && kill -s 14 $$ &
  28   # Waits 3 seconds, then sends sigalarm to script.
  29 }  
  30 
  31 
  32 Int14Vector()
  33 {
  34   answer="TIMEOUT"
  35   PrintAnswer
  36   exit $TIMER_INTERRUPT
  37 }  
  38 
  39 trap Int14Vector $TIMER_INTERRUPT
  40 # Timer interrupt (14) subverted for our purposes.
  41 
  42 echo "What is your favorite vegetable "
  43 TimerOn
  44 read answer
  45 PrintAnswer
  46 
  47 
  48 #  Admittedly, this is a kludgy implementation of timed input.
  49 #  However, the "-t" option to "read" simplifies this task.
  50 #  See the "t-out.sh" script.
  51 #  However, what about timing not just single user input,
  52 #+ but an entire script?
  53 
  54 #  If you need something really elegant ...
  55 #+ consider writing the application in C or C++,
  56 #+ using appropriate library functions, such as 'alarm' and 'setitimer.'
  57 
  58 exit 0

An alternative is using stty.


Example 9-3. Once more, timed input

   1 #!/bin/bash
   2 # timeout.sh
   3 
   4 #  Written by Stephane Chazelas,
   5 #+ and modified by the document author.
   6 
   7 INTERVAL=5                # timeout interval
   8 
   9 timedout_read() {
  10   timeout=$1
  11   varname=$2
  12   old_tty_settings=`stty -g`
  13   stty -icanon min 0 time ${timeout}0
  14   eval read $varname      # or just  read $varname
  15   stty "$old_tty_settings"
  16   # See man page for "stty."
  17 }
  18 
  19 echo; echo -n "What's your name? Quick! "
  20 timedout_read $INTERVAL your_name
  21 
  22 #  This may not work on every terminal type.
  23 #  The maximum timeout depends on the terminal.
  24 #+ (it is often 25.5 seconds).
  25 
  26 echo
  27 
  28 if [ ! -z "$your_name" ]  # If name input before timeout ...
  29 then
  30   echo "Your name is $your_name."
  31 else
  32   echo "Timed out."
  33 fi
  34 
  35 echo
  36 
  37 # The behavior of this script differs somewhat from "timed-input.sh."
  38 # At each keystroke, the counter resets.
  39 
  40 exit 0

Perhaps the simplest method is using the -t option to read.


Example 9-4. Timed read

   1 #!/bin/bash
   2 # t-out.sh [time-out]
   3 # Inspired by a suggestion from "syngin seven" (thanks).
   4 
   5 
   6 TIMELIMIT=4         # 4 seconds
   7 
   8 read -t $TIMELIMIT variable <&1
   9 #                           ^^^
  10 #  In this instance, "<&1" is needed for Bash 1.x and 2.x,
  11 #  but unnecessary for Bash 3+.
  12 
  13 echo
  14 
  15 if [ -z "$variable" ]  # Is null?
  16 then
  17   echo "Timed out, variable still unset."
  18 else  
  19   echo "variable = $variable"
  20 fi  
  21 
  22 exit 0

$UID

User ID number

Current user's user identification number, as recorded in /etc/passwd

This is the current user's real id, even if she has temporarily assumed another identity through su. $UID is a readonly variable, not subject to change from the command line or within a script, and is the counterpart to the id builtin.


Example 9-5. Am I root?

   1 #!/bin/bash
   2 # am-i-root.sh:   Am I root or not?
   3 
   4 ROOT_UID=0   # Root has $UID 0.
   5 
   6 if [ "$UID" -eq "$ROOT_UID" ]  # Will the real "root" please stand up?
   7 then
   8   echo "You are root."
   9 else
  10   echo "You are just an ordinary user (but mom loves you just the same)."
  11 fi
  12 
  13 exit 0
  14 
  15 
  16 # ============================================================= #
  17 # Code below will not execute, because the script already exited.
  18 
  19 # An alternate method of getting to the root of matters:
  20 
  21 ROOTUSER_NAME=root
  22 
  23 username=`id -nu`              # Or...   username=`whoami`
  24 if [ "$username" = "$ROOTUSER_NAME" ]
  25 then
  26   echo "Rooty, toot, toot. You are root."
  27 else
  28   echo "You are just a regular fella."
  29 fi

See also Example 2-3.

Note

The variables $ENV, $LOGNAME, $MAIL, $TERM, $USER, and $USERNAME are not Bash builtins. These are, however, often set as environmental variables in one of the Bash or login startup files. $SHELL, the name of the user's login shell, may be set from /etc/passwd or in an "init" script, and it is likewise not a Bash builtin.

 tcsh% echo $LOGNAME
 bozo
 tcsh% echo $SHELL
 /bin/tcsh
 tcsh% echo $TERM
 rxvt
 
 bash$ echo $LOGNAME
 bozo
 bash$ echo $SHELL
 /bin/tcsh
 bash$ echo $TERM
 rxvt
 	      

Positional Parameters

$0, $1, $2, etc.

Positional parameters, passed from command line to script, passed to a function, or set to a variable (see Example 4-5 and Example 15-16)

$#

Number of command-line arguments [4] or positional parameters (see Example 36-2)

$*

All of the positional parameters, seen as a single word

Note

"$*" must be quoted.

$@

Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without interpretation or expansion. This means, among other things, that each parameter in the argument list is seen as a separate word.

Note

Of course, "$@" should be quoted.


Example 9-6. arglist: Listing arguments with $* and $@

   1 #!/bin/bash
   2 # arglist.sh
   3 # Invoke this script with several arguments, such as "one two three" ...
   4 
   5 E_BADARGS=85
   6 
   7 if [ ! -n "$1" ]
   8 then
   9   echo "Usage: `basename $0` argument1 argument2 etc."
  10   exit $E_BADARGS
  11 fi  
  12 
  13 echo
  14 
  15 index=1          # Initialize count.
  16 
  17 echo "Listing args with \"\$*\":"
  18 for arg in "$*"  # Doesn't work properly if "$*" isn't quoted.
  19 do
  20   echo "Arg #$index = $arg"
  21   let "index+=1"
  22 done             # $* sees all arguments as single word. 
  23 echo "Entire arg list seen as single word."
  24 
  25 echo
  26 
  27 index=1          # Reset count.
  28                  # What happens if you forget to do this?
  29 
  30 echo "Listing args with \"\$@\":"
  31 for arg in "$@"
  32 do
  33   echo "Arg #$index = $arg"
  34   let "index+=1"
  35 done             # $@ sees arguments as separate words. 
  36 echo "Arg list seen as separate words."
  37 
  38 echo
  39 
  40 index=1          # Reset count.
  41 
  42 echo "Listing args with \$* (unquoted):"
  43 for arg in $*
  44 do
  45   echo "Arg #$index = $arg"
  46   let "index+=1"
  47 done             # Unquoted $* sees arguments as separate words. 
  48 echo "Arg list seen as separate words."
  49 
  50 exit 0

Following a shift, the $@ holds the remaining command-line parameters, lacking the previous $1, which was lost.
   1 #!/bin/bash
   2 # Invoke with ./scriptname 1 2 3 4 5
   3 
   4 echo "$@"    # 1 2 3 4 5
   5 shift
   6 echo "$@"    # 2 3 4 5
   7 shift
   8 echo "$@"    # 3 4 5
   9 
  10 # Each "shift" loses parameter $1.
  11 # "$@" then contains the remaining parameters.

The $@ special parameter finds use as a tool for filtering input into shell scripts. The cat "$@" construction accepts input to a script either from stdin or from files given as parameters to the script. See Example 16-24 and Example 16-25.

Caution

The $* and $@ parameters sometimes display inconsistent and puzzling behavior, depending on the setting of $IFS.


Example 9-7. Inconsistent $* and $@ behavior

   1 #!/bin/bash
   2 
   3 #  Erratic behavior of the "$*" and "$@" internal Bash variables,
   4 #+ depending on whether or not they are quoted.
   5 #  Demonstrates inconsistent handling of word splitting and linefeeds.
   6 
   7 
   8 set -- "First one" "second" "third:one" "" "Fifth: :one"
   9 # Setting the script arguments, $1, $2, $3, etc.
  10 
  11 echo
  12 
  13 echo 'IFS unchanged, using "$*"'
  14 c=0
  15 for i in "$*"               # quoted
  16 do echo "$((c+=1)): [$i]"   # This line remains the same in every instance.
  17                             # Echo args.
  18 done
  19 echo ---
  20 
  21 echo 'IFS unchanged, using $*'
  22 c=0
  23 for i in $*                 # unquoted
  24 do echo "$((c+=1)): [$i]"
  25 done
  26 echo ---
  27 
  28 echo 'IFS unchanged, using "$@"'
  29 c=0
  30 for i in "$@"
  31 do echo "$((c+=1)): [$i]"
  32 done
  33 echo ---
  34 
  35 echo 'IFS unchanged, using $@'
  36 c=0
  37 for i in $@
  38 do echo "$((c+=1)): [$i]"
  39 done
  40 echo ---
  41 
  42 IFS=:
  43 echo 'IFS=":", using "$*"'
  44 c=0
  45 for i in "$*"
  46 do echo "$((c+=1)): [$i]"
  47 done
  48 echo ---
  49 
  50 echo 'IFS=":", using $*'
  51 c=0
  52 for i in $*
  53 do echo "$((c+=1)): [$i]"
  54 done
  55 echo ---
  56 
  57 var=$*
  58 echo 'IFS=":", using "$var" (var=$*)'
  59 c=0
  60 for i in "$var"
  61 do echo "$((c+=1)): [$i]"
  62 done
  63 echo ---
  64 
  65 echo 'IFS=":", using $var (var=$*)'
  66 c=0
  67 for i in $var
  68 do echo "$((c+=1)): [$i]"
  69 done
  70 echo ---
  71 
  72 var="$*"
  73 echo 'IFS=":", using $var (var="$*")'
  74 c=0
  75 for i in $var
  76 do echo "$((c+=1)): [$i]"
  77 done
  78 echo ---
  79 
  80 echo 'IFS=":", using "$var" (var="$*")'
  81 c=0
  82 for i in "$var"
  83 do echo "$((c+=1)): [$i]"
  84 done
  85 echo ---
  86 
  87 echo 'IFS=":", using "$@"'
  88 c=0
  89 for i in "$@"
  90 do echo "$((c+=1)): [$i]"
  91 done
  92 echo ---
  93 
  94 echo 'IFS=":", using $@'
  95 c=0
  96 for i in $@
  97 do echo "$((c+=1)): [$i]"
  98 done
  99 echo ---
 100 
 101 var=$@
 102 echo 'IFS=":", using $var (var=$@)'
 103 c=0
 104 for i in $var
 105 do echo "$((c+=1)): [$i]"
 106 done
 107 echo ---
 108 
 109 echo 'IFS=":", using "$var" (var=$@)'
 110 c=0
 111 for i in "$var"
 112 do echo "$((c+=1)): [$i]"
 113 done
 114 echo ---
 115 
 116 var="$@"
 117 echo 'IFS=":", using "$var" (var="$@")'
 118 c=0
 119 for i in "$var"
 120 do echo "$((c+=1)): [$i]"
 121 done
 122 echo ---
 123 
 124 echo 'IFS=":", using $var (var="$@")'
 125 c=0
 126 for i in $var
 127 do echo "$((c+=1)): [$i]"
 128 done
 129 
 130 echo
 131 
 132 # Try this script with ksh or zsh -y.
 133 
 134 exit 0
 135 
 136 #  This example script written by Stephane Chazelas,
 137 #+ and slightly modified by the document author.

Note

The $@ and $* parameters differ only when between double quotes.


Example 9-8. $* and $@ when $IFS is empty

   1 #!/bin/bash
   2 
   3 #  If $IFS set, but empty,
   4 #+ then "$*" and "$@" do not echo positional params as expected.
   5 
   6 mecho ()       # Echo positional parameters.
   7 {
   8 echo "$1,$2,$3";
   9 }
  10 
  11 
  12 IFS=""         # Set, but empty.
  13 set a b c      # Positional parameters.
  14 
  15 mecho "$*"     # abc,,
  16 #                   ^^
  17 mecho $*       # a,b,c
  18 
  19 mecho $@       # a,b,c
  20 mecho "$@"     # a,b,c
  21 
  22 #  The behavior of $* and $@ when $IFS is empty depends
  23 #+ on which Bash or sh version being run.
  24 #  It is therefore inadvisable to depend on this "feature" in a script.
  25 
  26 
  27 # Thanks, Stephane Chazelas.
  28 
  29 exit

Other Special Parameters

$-

Flags passed to script (using set). See Example 15-16.

Caution

This was originally a ksh construct adopted into Bash, and unfortunately it does not seem to work reliably in Bash scripts. One possible use for it is to have a script self-test whether it is interactive.

$!

PID (process ID) of last job run in background

   1 LOG=$0.log
   2 
   3 COMMAND1="sleep 100"
   4 
   5 echo "Logging PIDs background commands for script: $0" >> "$LOG"
   6 # So they can be monitored, and killed as necessary.
   7 echo >> "$LOG"
   8 
   9 # Logging commands.
  10 
  11 echo -n "PID of \"$COMMAND1\":  " >> "$LOG"
  12 ${COMMAND1} &
  13 echo $! >> "$LOG"
  14 # PID of "sleep 100":  1506
  15 
  16 # Thank you, Jacques Lederer, for suggesting this.

Using $! for job control:

   1 possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
   2 # Forces completion of an ill-behaved program.
   3 # Useful, for example, in init scripts.
   4 
   5 # Thank you, Sylvain Fourmanoit, for this creative use of the "!" variable.

Or, alternately:

   1 # This example by Matthew Sage.
   2 # Used with permission.
   3 
   4 TIMEOUT=30   # Timeout value in seconds
   5 count=0
   6 
   7 possibly_hanging_job & {
   8         while ((count < TIMEOUT )); do
   9                 eval '[ ! -d "/proc/$!" ] && ((count = TIMEOUT))'
  10                 # /proc is where information about running processes is found.
  11                 # "-d" tests whether it exists (whether directory exists).
  12                 # So, we're waiting for the job in question to show up.
  13                 ((count++))
  14                 sleep 1
  15         done
  16         eval '[ -d "/proc/$!" ] && kill -15 $!'
  17         # If the hanging job is running, kill it.
  18 }
  19 
  20 #  -------------------------------------------------------------- #
  21 
  22 #  However, this may not not work as specified if another process
  23 #+ begins to run after the "hanging_job" . . .
  24 #  In such a case, the wrong job may be killed.
  25 #  Ariel Meragelman suggests the following fix.
  26 
  27 TIMEOUT=30
  28 count=0
  29 # Timeout value in seconds
  30 possibly_hanging_job & {
  31 
  32 while ((count < TIMEOUT )); do
  33   eval '[ ! -d "/proc/$lastjob" ] && ((count = TIMEOUT))'
  34   lastjob=$!
  35   ((count++))
  36   sleep 1
  37 done
  38 eval '[ -d "/proc/$lastjob" ] && kill -15 $lastjob'
  39 
  40 }
  41 
  42 exit

$_

Special variable set to final argument of previous command executed.


Example 9-9. Underscore variable

   1 #!/bin/bash
   2 
   3 echo $_              #  /bin/bash
   4                      #  Just called /bin/bash to run the script.
   5                      #  Note that this will vary according to
   6                      #+ how the script is invoked.
   7 
   8 du >/dev/null        #  So no output from command.
   9 echo $_              #  du
  10 
  11 ls -al >/dev/null    #  So no output from command.
  12 echo $_              #  -al  (last argument)
  13 
  14 :
  15 echo $_              #  :

$?

Exit status of a command, function, or the script itself (see Example 24-7)

$$

Process ID (PID) of the script itself. [5] The $$ variable often finds use in scripts to construct "unique" temp file names (see Example 32-6, Example 16-31, and Example 15-27). This is usually simpler than invoking mktemp.

Notes

[1]

A stack register is a set of consecutive memory locations, such that the values stored (pushed) are retrieved (popped) in reverse order. The last value stored is the first retrieved. This is sometimes called a LIFO (last-in-first-out) or pushdown stack.

[2]

The PID of the currently running script is $$, of course.

[3]

Somewhat analogous to recursion, in this context nesting refers to a pattern embedded within a larger pattern. One of the definitions of nest, according to the 1913 edition of Webster's Dictionary, illustrates this beautifully: "A collection of boxes, cases, or the like, of graduated size, each put within the one next larger."

[4]

The words "argument" and "parameter" are often used interchangeably. In the context of this document, they have the same precise meaning: a variable passed to a script or function.

[5]

Within a script, inside a subshell, $$ returns the PID of the script, not the subshell.