Bash Special Variables in Linux

Bash (Bourne Again SHell) is the default shell for most Linux systems. It is a command language interpreter that executes commands from standard input, files, or command-line arguments. Bash provides a set of special variables that contain various system-related and user-related information. These variables are automatically set by the shell and provide crucial data for script execution and system monitoring.

What are Bash Special Variables?

Bash special variables are predefined variables that store system and user-related information. They are prefixed with the $ symbol and are automatically updated by the shell. These variables are essential for creating robust bash scripts and accessing runtime information.

Variable Description Example Use Case
$0 Script/shell name Display current script name
$# Number of arguments Validate argument count
$* All arguments as single string Pass arguments to functions
$@ All arguments as separate strings Loop through arguments
$? Exit status of last command Error checking
$$ Current process ID Create unique filenames
$! Last background process ID Monitor background jobs

Script Name and Arguments

$0 Script Name

The $0 variable contains the name of the currently executing script or shell.

#!/bin/bash
echo "Script name: $0"
echo "Script basename: $(basename $0)"
Script name: ./myscript.sh
Script basename: myscript.sh

$# Argument Count

The $# variable stores the number of command-line arguments passed to the script.

#!/bin/bash
echo "Number of arguments: $#"
if [ $# -lt 2 ]; then
    echo "Usage: $0 <arg1> <arg2>"
    exit 1
fi

Running with ./script.sh hello world:

Number of arguments: 2

$* vs $@ All Arguments

Both store all arguments, but behave differently when quoted:

#!/bin/bash
echo "Using \$*: $*"
echo "Using \$@: $@"

# Demonstrate difference when quoted
for arg in "$*"; do
    echo "Star: [$arg]"
done

for arg in "$@"; do
    echo "At: [$arg]"
done

Running with ./script.sh "hello world" test:

Using $*: hello world test
Using $@: hello world test
Star: [hello world test]
At: [hello world]
At: [test]

Process Information

$$ Current Process ID

#!/bin/bash
echo "Current script PID: $$"
echo "Creating temp file: /tmp/data.$$"
touch "/tmp/data.$$"
Current script PID: 1929919
Creating temp file: /tmp/data.1929919

$! Background Process ID

#!/bin/bash
sleep 10 &
echo "Background process PID: $!"
echo "Monitoring process..."
wait $!
echo "Background process completed"
Background process PID: 12346
Monitoring process...
Background process completed

Exit Status and Error Handling

$? Exit Status

The $? variable contains the exit status (0-255) of the last executed command, where 0 indicates success.

#!/bin/bash
ls /existing/directory
echo "ls exit status: $?"

ls /nonexistent/directory 2>/dev/null
echo "ls exit status: $?"

# Error handling example
if [ $? -ne 0 ]; then
    echo "Previous command failed"
fi
file1.txt  file2.txt
ls exit status: 0
ls exit status: 2
Previous command failed

Advanced Special Variables

$IFS Internal Field Separator

Controls how bash splits strings into fields. Default is space, tab, and newline.

#!/bin/bash
data="apple,banana,cherry"
IFS=','
read -ra fruits <<< "$data"
for fruit in "${fruits[@]}"; do
    echo "Fruit: $fruit"
done
Fruit: apple
Fruit: banana
Fruit: cherry

$LINENO Current Line Number

#!/bin/bash
echo "This is line number: $LINENO"
echo "This is line number: $LINENO"
This is line number: 2
This is line number: 3

$SECONDS Script Runtime

#!/bin/bash
echo "Script started"
sleep 3
echo "Elapsed time: $SECONDS seconds"
Script started
Elapsed time: 3 seconds

Common Use Cases

  • Argument validation Use $# to check if correct number of arguments provided

  • Error handling Use $? to check command success/failure

  • Unique filenames Use $$ to create process-specific temporary files

  • Background job management Use $! to track background processes

  • Performance monitoring Use $SECONDS to measure execution time

  • Debugging Use $LINENO to identify problem locations in scripts

Conclusion

Bash special variables are fundamental tools for effective shell scripting in Linux. They provide automatic access to runtime information, process data, and command results. Understanding these variables enables you to write more robust, error-aware scripts that can handle various execution scenarios and provide meaningful feedback to users.

Updated on: 2026-03-17T09:01:38+05:30

541 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements