In Bash, special variables are predefined variables that hold specific values or provide information about the environment and execution of the script. These variables are automatically set by the shell and can be accessed and used within your script.
Here are some commonly used special variables in Bash:
$0: Represents the name of the script or the shell itself.
$3, …: These variables hold the positional parameters passed to the script or function.
$1represents the first parameter,
$2represents the second parameter, and so on.
$#: Holds the number of positional parameters passed to the script or function.
$@: Represents all the positional parameters passed to the script or function as separate arguments.
$*: Represents all the positional parameters passed to the script or function as a single string.
$$: Holds the process ID (PID) of the currently running script.
$?: Represents the exit status of the last executed command. A value of 0 typically indicates success, while non-zero values indicate an error.
$!: Holds the process ID (PID) of the last background command executed.
$USER: Represents the username of the current user.
$HOME: Holds the home directory path of the current user.
$PWD: Represents the current working directory.
These are just a few examples of special variables in Bash. There are additional special variables available, each serving a specific purpose. You can refer to the Bash documentation or resources on Bash scripting to explore the complete list of special variables and their usage.
Special variables provide information about the script’s execution environment, command-line arguments, and other relevant details. They allow you to write more dynamic and adaptable scripts by accessing and utilizing this information as needed.