Jay Taylor's notes

back to listing index

bash - What are the special dollar sign shell variables? - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: syntax bash shell-scripting dollar variables stackoverflow.com
Clipped on: 2020-05-20

Asked 9 years, 2 months ago
Viewed 319k times

In Bash, there appear to be several variables which hold special, consistently-meaning values. For instance,

./myprogram &; echo $!

will return the PID of the process which backgrounded myprogram. I know of others, such as $? which I think is the current TTY. Are there others?

  • $1, $2, $3, ... are the positional parameters.
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
  • "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
  • $# is the number of positional parameters.
  • $- current options set for the shell.
  • $$ pid of the current shell (not subshell).
  • $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
  • $IFS is the (input) field separator.
  • $? is the most recent foreground pipeline exit status.
  • $! is the PID of the most recent background command.
  • $0 is the name of the shell or shell script.

Most of the above can be found under Special Parameters in the Bash Reference Manual. There are all the environment variables set by the shell.

For a comprehensive index, please see the Reference Manual Variable Index.

Image (Asset 2/9) alt=
  • 4
    @chepner look in man(1) bash under Special Parameters for the rest of the definition of $_. – kojiro Jul 17 '12 at 12:52
  • 8
    I use !$ instead of $_ in bash scripts, because the latter sometimes fails. – amc Sep 3 '12 at 3:57
  • Nice list kojiro. I ran across this syntax ${1+"$@"} what does that mean? – user178047 Mar 7 '14 at 21:54
  • 3
    @amc: See unix.stackexchange.com/questions/271659/… for differences between !$ and $_. – tricasse Apr 20 '17 at 17:54
  • 44
    • $_ last argument of last command
    • $# number of arguments passed to current script
    • $* / $@ list of arguments passed to script as string / delimited list

    off the top of my head. Google for bash special variables.

    answered Mar 2 '11 at 3:46
    Image (Asset 3/9) alt=
  • 1
    I think $@ is the string and $* is the delimited list (according to the above accepted answer, anyways). – RastaJedi Aug 20 '16 at 19:02
  • 1
    @RastaJedi: "$@" expands to a list, "$*" expands to a single string. The special behavior of $@ applies when it's within double quotes. – Keith Thompson Apr 5 '18 at 16:25
  • 6
    Downvoted because this is not a very complete or helpful answer. Saying "Google it" shows a lack of effort, especially considering that the vast majority of people end up here from Google search. I think the least you could do is add links to official documentation. TBH, the only reason I didn't flag it for deletion was because it is at least a partial answer to the question. I know it's old, but please consider editing it to provide more detail. – Sean the Bean Sep 11 '18 at 20:17
  • 15

    To help understand what do $#, $0 and $1, ..., $n do, I use this script:

    for ((i=0; i<=$#; i++)); do
      echo "parameter $i --> ${!i}"

    Running it returns a representative output:

    $ ./myparams.sh "hello" "how are you" "i am fine"
    parameter 0 --> myparams.sh
    parameter 1 --> hello
    parameter 2 --> how are you
    parameter 3 --> i am fine
    answered Apr 25 '16 at 12:46
    Image (Asset 4/9) alt=

    Take care with some of the examples; $0 may include some leading path as well as the name of the program. Eg save this two line script as ./mytry.sh and the execute it.

    echo "parameter 0 --> $0" ; exit 0


    parameter 0 --> ./mytry.sh

    This is on a current (year 2016) version of Bash, via Slackware 14.2

    Image (Asset 5/9) alt= Sign up using Google
    Sign up using Facebook
    Sign up using Email and Password

    Post as a guest


    Required, but never shown

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.