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 /home

You 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:

ParameterMeaning
$0Script name
$1-$9Arguments
$#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.sh

Then run it:

./test.sh coffee beans

Study 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:

Leave a comment

Your email address will not be published. Required fields are marked *