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.

Advertisement

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. `$@` – 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:

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

$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.

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.

$* – 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.

$@ – All command-line arguments as an array

The $@ 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 $@ variable to iterate over each directory name and list the files in that directory.

$? – 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.

$$ – 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.

$! – 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.

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.

Share.
Leave A Reply

Exit mobile version