This post may contain affiliate links, please read our affiliate disclosure to learn more.
What the $!$*$@ … Bash Shell Variables

What the $!$*$@ … Bash Shell Variables

Author
 By Charles Joseph | Cybersecurity Researcher
Clock
 Published on December 26th, 2023
This post was updated on February 29th, 2024

There’s nothing complex about Bash shell variables, but they’re also not something you’d likely memorize. Frankly, they’re just not that interesting. I wrote the following *nix script to demonstrate how these variables work. For me, it’s easier to understand the variables using an example.

Custom Script

#!/bin/bash
# the following command is executed to get a process ID for $!
seq 1000000 | sort -nr > /dev/null &
echo “\$*: $*”
echo “\$@: $@”
echo “\$#: $#”
echo “\$?: $?”
echo “\$-: $-“
echo “\$\$: $$”
echo “\$!: $!”
echo “\$0: $0”
echo “\$_: $_”

NordVPN 67% off + 3-month VPN coupon

Stay One Step Ahead of Cyber Threats

Want to Be the Smartest Guy in the Room? Get the Latest Cybersecurity News and Insights.
We respect your privacy and you can unsubscribe anytime.

Notice the two parameters I’m passing at the command line — it’s only 2 arguments. Look carefully at the quotes, which in total include 3 words.

Script Output

$ ./shell-special-params.sh “I love” “handsonkeyboard.com”
$*: I love handsonkeyboard.com
$@: I love handsonkeyboard.com
$#: 2
$?: 0
$-: hB
$$: 18414
$!: 18416
$0: ./shell-special-params.sh
$_: $0: ./shell-special-params.sh

VariableDescriptionValue (based on the executed script above)
$*This parameter represents all arguments passed to the script, separated using the first character of the special shell variable IFS — the internal field separator.I love handsonkeyboard.com
$@This parameter represents all arguments passed to the script separated as words.I love handsonkeyboard.com
$#The number of arguments passed to the script.2
$?The exit status of the most recently executed foreground pipeline.0
$-This is from the Linux Documentation Project’s article on Linux variables:
“The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.”
hB
$$The process ID of the shell.18414
$!The process ID of the most recently executed background command.18416
$0The name of the shell script../shell-special-params.sh
$_This is from the Linux Documentation Project’s article on linux-variables:
“The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.”
$0: ./shell-special-params.sh
QUOTE:
"Amateurs hack systems, professionals hack people."
-- Bruce Schneier, a renown computer security professional
Scroll to Top