Bash is a powerful shell that provides a wide range of special variables that can be used to manipulate and control the behavior of scripts. These variables provide essential information about the environment in which the script is running, including the command-line arguments, the current process ID, and the status of the last executed command.

In this article, we’ll provide an in-depth guide to all bash special variables, including examples of their usage and common pitfalls to avoid.

  1. `$0` – The name of the script being executed.
  2. `$1-$9` – The first nine command-line arguments.
  3. `$#` – The number of command-line arguments.
  4. `$*` – All command-line arguments as a single string.
  5. `[email protected]` – All command-line arguments as an array.
  6. `$?` – The exit status of the last executed command.
  7. `$$` – The process ID of the current shell.
  8. `$!` – The process ID of the last background command.

Let’s discuss the special variables in detail with examples.

$0 – The name of the script being executed

In bash, $0 is a special parameter that holds the name of the script or shell that is currently being executed. It is also known as the “name” or “zeroth argument” of the script.

For example, suppose you have a script called “myscript.sh” that you want to run from any directory. In that case, you can use the $0 variable to determine the name of the script being executed:

#!/bin/bash

echo “The current running script is: $0”

You can also determine the directory in which the script is located and then use that directory to locate any files the script needs.

#!/bin/bash

SCRIPT_DIR=“$( cd “$( dirname “${BASH_SOURCE[0]}” )” && pwd )”

echo “The script is running from $SCRIPT_DIR”

$1, $2, …, $9 – Command-line arguments

The $1, $2, …, $9 variables contain the first nine command-line arguments passed to the script. These variables are useful for creating shell scripts that accept user input.

For example, suppose you have a script called “greet.sh” that takes a name as its first command-line argument. In that case, you can use the $1 variable to retrieve the name and then use it in the script’s output.

#!/bin/bash

echo “Hello, $1!”

If a script needs to access more than nine command-line arguments, the ${10}, ${11}, …, ${N} variables can be used to retrieve them. These variables can be used with the shift command to process command-line arguments in batches. Make sure that the variable is enclosed with {} brackets.

$# – The number of command-line arguments

The $# variable contains the number of command-line arguments passed to the script. This variable is useful for creating shell scripts that validate user input.

For example, suppose you have a script called “validate.sh” that requires two command-line arguments. In that case, you can use the $# variable to ensure that the correct number of arguments is provided.

#!/bin/bash

if [[ $# -ne 2 ]]

then

    echo “Usage: validate.sh [username] [password]”

    exit 1

fi

$* – All command-line arguments as a single string

The $* variable contains all command-line arguments passed to the script as a single string. This variable is useful for creating shell scripts that need to manipulate the entire command-line string.

For example, suppose you have a script called “join.sh” that joins two strings provided as command-line arguments. In that case, you can use the $* variable to concatenate the strings.

[email protected] – All command-line arguments as an array

The [email protected] variable contains all command-line arguments passed to the script as an array. This variable is useful for creating shell scripts that need to manipulate individual command-line arguments.

For example, suppose you have a script called “list.sh” that lists all files in a directory provided as a command-line argument. In that case, you can use the [email protected] variable to iterate over each directory name and list the files in that directory.

#!/bin/bash

do

    echo “Listing files in $directory:”

    ls l $directory

done

$? – The exit status of the last executed command

The $? variable contains the exit status of the last executed command. This variable is helpful in creating shell scripts that need to handle errors or take different actions depending on the success or failure of a command.

For example, suppose you have a script called “create-file.sh” that creates a file and returns an exit status indicating success or failure. In that case, you can use the $? variable to check the exit status and take appropriate action.

#!/bin/bash

touch myfile.txt

if [[ $? eq 0 ]]

then

    echo “File created successfully”

else

    echo “Error creating file”

fi

$$ – The process ID of the current shell

The $$ variable contains the process ID of the current script. This variable is useful for creating shell scripts that need to manage multiple processes or create unique file names.

For example, suppose you have a script called “log.sh” that logs information to a file with a unique name based on the process ID. In that case, you can use the $$ variable to generate a unique file name.

#!/bin/bash

LOG_FILE=“log_$$.txt”

echo “Logging to file $LOG_FILE”

$! – The process ID of the last background command

The $! variable contains the process ID of the last background command executed by the script. This variable is useful for creating shell scripts that need to manage multiple processes or monitor the progress of long-running commands.

For example, suppose you have a script called “background.sh” that runs a command in the background and logs its progress. In that case, you can use the $! variable to monitor the progress of the command.

#!/bin/bash

mycommand &

pid=$!

while ps -p $pid >/dev/null; do

    echo “Command is still running”

    sleep 1

done

echo “Command has finished”

Conclusion

In conclusion, bash special variables provide essential information about the environment in which the script is running. These variables enable you to create more flexible, robust, and error-resistant shell scripts. By mastering the usage of these variables, you can take your shell scripting skills to the next level.