Special Variables: Shell Provides These
Shell provides special variables. They're always available. Use them.
Here's the thing: Special variables are useful. Learn them. Use them.
Script Information
$0: Script Name
echo "Script: $0"
My take: $0 is the script name. Use it for logging.
$#: Argument Count
echo "Arguments: $#"
My take: $# is argument count. Check it before using arguments.
Arguments
$1, $2, $3...: Positional Arguments
echo "First: $1"
echo "Second: $2"
My take: $1 is first argument. $2 is second. Simple.
$@: All Arguments
echo "All: $@"
My take: $@ is all arguments. Use it to pass them along.
$*: All Arguments (Different)
echo "All: $*"
My take: $* is similar but different. Use $@ usually.
Process Information
$$: Process ID
echo "PID: $$"
My take: $$ is the process ID. Use it for temp files.
$?: Exit Code
command
if [ $? -eq 0 ]; then
echo "Success"
fi
My take: $? is exit code. Check it after commands.
Common Patterns
Check Arguments
if [ $# -eq 0 ]; then
echo "Usage: $0 <arg>"
exit 1
fi
Use All Arguments
for arg in "$@"; do
echo "$arg"
done
What's Next?
Now that you understand special variables, let's talk about Environment Variables.
Personal note: Special variables seemed confusing at first. Then I learned them. Now I use them constantly. They're useful. Learn them.