Shell Variables
Dollar Sign Variables
Programming
Shell Scripting
Unix/Linux

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.

Browse interview questions

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:

  1. $? - Exit Status of Last Command
    • Represents the exit status of the last command executed. An exit status of 0 typically means the command was successful, whereas any non-zero value indicates an error.
  2. $# - 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.
  3. $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.
  4. $1, $2, ..., ${10}, ${11}, ... - Positional Parameters
    • These variables represent the arguments provided to the script. $1 is the first argument, $2 the second, and so on. For numbers 10 and above, the braces {} are necessary.
  5. $$ - 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.
  6. $! - 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.
  7. $* 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.
  8. $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:

bash
1#!/bin/bash
2echo "Script Name: $0"
3echo "Number of Arguments: $#"
4echo "First Argument: $1"
5echo "Process ID: $$"
6if [ $# -eq 0 ]; then
7  echo "No arguments provided."
8else
9  echo "Arguments provided!"
10fi
11exit 0

Summary Table

VariableDescriptionExample
$?Exit status of the last executed command.echo $?
$#Number of command-line arguments.echo $#
$0Name 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 "$@"
$IFSInternal 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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions