What are the special dollar sign shell variables?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the context of Unix-like operating systems, shell environments such as Bash provide special variables that utilize the dollar sign ($) as a prefix. These variables are integral to shell scripting and command line operations, as they can store data about the script environment, keep track of process statuses, and facilitate dynamic operations. Below, we'll explore some critically important special dollar sign variables, their purposes, and examples of how they can be used.
Key Special Dollar Sign Variables
Here's a brief overview of some of the most commonly used special dollar sign shell variables:
$?- Exit Status of Last Command- Represents the exit status of the last command executed. An exit status of
0typically means the command was successful, whereas any non-zero value indicates an error.
$#- Number of Command-line Arguments- Returns the number of command-line arguments passed to the script. This is useful for scripts that require variable numbers of inputs.
$0- The Filename of the Current Script- Gives the name of the running script. This can be used when you need to output the name of the script itself within error messages or logs.
$1, $2, ..., ${10}, ${11}, ...- Positional Parameters- These variables represent the arguments provided to the script.
$1is the first argument, $2the second, and so on. For numbers 10 and above, the braces{}are necessary.
$$- Process ID of the Script- Provides the process ID of the script instance that is currently executing. This is particularly useful for logging or when you need to uniquely identify the script process for other operations like sending signals.
$!- Process ID of the Last Background Command- Contains the PID of the most recently executed background command. This can be handy for monitoring or controlling background processes.
$*and$@- All Positional Parameters- Both variables hold all the positional parameters (1, 2, etc.). The difference becomes notable in how they behave in quotes.
$*consolidates them into a single word, whereas$@treats them as separate words.
$IFS- Internal Field Separator- This special variable determines how Bash recognizes field boundaries. Its default value includes space, tab, and newline. This is crucial when formatting output or processing text data.
Examples of Usage
Here's a simple script demonstrating the use of some of these variables:
Summary Table
| Variable | Description | Example |
$? | Exit status of the last executed command. | echo $? |
$# | Number of command-line arguments. | echo $# |
$0 | Name of the current script. | echo $0 |
$1... | Positional parameters of the script. | echo $1 |
$$ | Process ID of the current shell instance. | echo $$ |
$! | Process ID of the last background command. | echo $! |
$* | All positional parameters as a single string. | echo "$*" |
$@ | All positional parameters as separate strings. | echo "$@" |
$IFS | Internal Field Separator. | echo "$IFS" |
Conclusion
Understanding and leveraging these special dollar sign shell variables is key to mastering shell scripting and effective command-line use. By integrating these variables into your scripts, you can handle dynamic inputs, monitor and control process behaviors, and tailor the script's execution behavior based on context-specific criteria. Whether you're a novice scripter or an experienced system administrator, these variables offer powerful tools to enhance the functionality and robustness of your scripts.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.