What are Bash special parameters is a topic I have been running away from for a while. Not anymore! I finally started getting more into bash scripting basics.
Special parameters are predefined variables that Bash automatically creates. You don’t assign values to them. This was something new to me. So, after brewing a good cup of Latin America coffee I decided to write this simple guide about bash special parameters.
Below are the usual examples for bash special parameters in Linux:
$0
$1
$2
$#
$?
$$
$!
$*
$@
They provide information about:
- the script
- command arguments
- process IDs
- exit status
- background jobs
As a Linux admin this is useful for automate backups, monitor services, manage users, log files, restart things that fail. Without special parameters, scripts become hard-coded and inflexible. For example:
tar -czf backup.tar.gz /homeYou can write:
tar -czf "$1" "$2"Where you can pass the values using a script:
tar -czf <thebackup.tar.gz> <what-to-backup>Below is a good table to remember about bash special parameters:
| Parameter | Meaning |
|---|---|
$0 | Script name |
$1-$9 | Arguments |
$# | Number of arguments |
$? | Exit status |
$$ | Current shell PID |
$! | Last background PID |
$* | All arguments as one string |
$@ | All arguments separately |
Create a practice .sh file. For example test.sh and place the code below:
#!/bin/bash
echo "Script Name: $0"
echo "Argument Count: $#"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Exit Status: $?"
echo "Current PID: $$"Make it executable:
chmod +x test.shThen run it:
./test.sh coffee beansStudy the output and let me know if you have any questions!
Remember, I create unique IT Handyman accessories. See them below and if you like to support me place an order:
Recommended products
apt install Coffee Mug
$19.95Code with Coffee Short-Sleeve Unisex T-Shirt
Price range: $24.00 through $28.00Holiday Bash Coffee Mug for $bash lovers 11oz
$20.95Coffee First Then ssh Black Mug
$16.00





