What do `$0`, `$1`, `$#`, `$@`, `$*`, `$?`, `$$`, `$!` and `$-` contain?
BeginnerConceptShells, scripts and execution
Answer
| Variable | Meaning |
|---|---|
$0 |
Script name as invoked |
$1..$9, ${10} |
Positional arguments (braces needed from 10) |
$# |
Number of arguments |
"$@" |
All arguments, each as a separate word |
"$*" |
All arguments joined into one word with the first char of IFS |
$? |
Exit status of the last command |
$$ |
PID of the current shell (the parent, even inside a subshell) |
$! |
PID of the last background job |
$- |
Current option flags, e.g. hB or ehuB |
$BASHPID gives the real PID inside a subshell.
More on Shells, scripts and execution
What is a shell, and how is Bash different from sh, dash and zsh?BeginnerWhat does the shebang line do, and why prefer `#!/usr/bin/env bash` over `#!/bin/bash`?BeginnerWhat is the difference between `./script.sh`, `bash script.sh` and `source script.sh`?BeginnerWhat happens when you type a command name? How does Bash decide what to run?AdvancedWhy must `cd`, `export` and `read` be builtins rather than external programs?IntermediateWhat is the difference between a login, interactive and non-interactive shell, and which startup files run?Intermediate