What are the special dollar sign shell variables?
Asked Answered
L

4

948

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?

Limelight answered 2/3, 2011 at 3:42 Comment(6)
Several of them are not Bash-only. They're also used in other Bourne-related shells and in fact are specified by POSIX.Abdominal
What about: IFS=$'\n' See: #4128735Hiedihiemal
@Hiedihiemal That's not a parameter; that's a special type of quoting. $'\n' is a literal newline character that result from replacing the digraph \n with ASCII 10.Amorete
If you came here looking for ${1}, ${*}, etc, the braces are just for disambiguation, and often redundant. In isolation, ${x} is exactly equivalent to $x.Outsail
for $IFS see What is the exact meaning of IFS=$'\n'Glass
$? is not the TTY - it is the return value of the last executed command.tty is the simple command for it.Ditchwater
P
1662
  • $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. Here are all the environment variables set by the shell.

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

Pneumograph answered 2/3, 2011 at 4:4 Comment(11)
They are all documented in the bash man page. The only oddity is that $_ is only mentioned in the context of its use in the MAILPATH variable.Amorete
@Amorete look in man(1) bash under Special Parameters for the rest of the definition of $_.Pneumograph
I use !$ instead of $_ in bash scripts, because the latter sometimes fails.Onofredo
@amc: See unix.stackexchange.com/questions/271659/… for differences between !$ and $_.Daemon
see this thread for a discussion on $(...) operationsFiredamp
What is the variable for the PID of the last command, not background?Ditchwater
@Ditchwater how can you ask the shell for a pid if the command is not in the background?Pneumograph
@kojiro, thanks, with history, used in atop. But it stores it in a file, so there is no $.. variable.Ditchwater
$$ doubles up as a good random number generator! Well, sort of random.Reign
is there any difference where you used double inverted comas? I am referring to "$@" and "$*".Shornick
@Pneumograph Oups, I got it mixed up the the wrong way round. I have deleted my previous comment.Fulfill
P
53
  • $_ 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.

Potvaliant answered 2/3, 2011 at 3:46 Comment(4)
"I did. They sent me here." -- Bookshop skit (official doc: gnu.org/software/bash/manual/bashref.html#Special-Parameters)Colloidal
I think $@ is the string and $* is the delimited list (according to the above accepted answer, anyways).Luzon
@RastaJedi: "$@" expands to a list, "$*" expands to a single string. The special behavior of $@ applies when it's within double quotes.Philip
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.Clearway
G
27

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

#!/bin/bash

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

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
Graaf answered 25/4, 2016 at 12:46 Comment(2)
What does the !i mean in your for loop? That's actually why I'm here and I can't find an answer anywhere because I don't know what to call it.Fichte
@Fichte this is called variable indirection. You can find a good explantion of his usage in bash: indirect expansion, please explain?.Graaf
D
4

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.

#!/bin/bash

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

Output:

parameter 0 --> ./mytry.sh

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

Diplostemonous answered 14/9, 2016 at 10:19 Comment(1)
Whether $0 does include a path or not depends on how you ran the script in the first place. If you executed "./mytry.sh" that's what you will see in $0. If you entered "~/mytry.sh" you will see the full path (because the shell will have expanded ~). If you did ". mytry.sh" you will see 'bash'.Competency

© 2022 - 2024 — McMap. All rights reserved.