Skip to main content

Command Palette

Search for a command to run...

Special Variables in Shell Scripting: Small Symbols, Powerful Impact

Updated
โ€ข4 min read

๐—จ๐—ป๐—ฑ๐—ฒ๐—ฟ๐˜€๐˜๐—ฎ๐—ป๐—ฑ๐—ถ๐—ป๐—ด ๐—ฆ๐—ฝ๐—ฒ๐—ฐ๐—ถ๐—ฎ๐—น ๐—ฉ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ๐˜€ ๐—ถ๐—ป ๐—ฆ๐—ต๐—ฒ๐—น๐—น ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜๐—ถ๐—ป๐—ด

While learning Shell Scripting, I came across something very useful: ๐—ฆ๐—ฝ๐—ฒ๐—ฐ๐—ถ๐—ฎ๐—น ๐—ฉ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ๐˜€.

These variables are automatically provided by the shell and are used to access script arguments, process information, user details, and command execution results.

They play an important role in making Shell Scripts dynamic by handling inputs, processes, users, and execution results.

They help us:

โ€ข Read script arguments

โ€ข Identify who is running the script

โ€ข Track process information

โ€ข Monitor command execution status

โ€ข Access system and environment details

Let's look at some commonly used special variables.

1๏ธโƒฃ $๐Ÿญ, $๐Ÿฎ, ..., $๐—ก โ€“ ๐—ฃ๐—ผ๐˜€๐—ถ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฃ๐—ฎ๐—ฟ๐—ฎ๐—บ๐—ฒ๐˜๐—ฒ๐—ฟ๐˜€

These represent arguments passed to a script. $1 is the first argument passed to the script, $2 is the second, and so on.. If you pass parameters while running the script, these will automatically capture them.

Example:

./script.sh DevOps Linux

Inside the script:

echo "$1"

echo "$2"

Output:

DevOps Linux

2๏ธโƒฃ $@ ๐—ฎ๐—ป๐—ฑ $* โ€“ ๐—”๐—น๐—น ๐—”๐—ฟ๐—ด๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€

Both represent all arguments passed to the script.

Difference:

  • $@ treats arguments individually

  • $* treats all arguments as a single string.

Example

./script.sh DevOps Linux Cloud

echo "$@"

echo "$*"

Output:

DevOps Linux Cloud

DevOps Linux Cloud

3๏ธโƒฃ$# โ€“ ๐—ก๐˜‚๐—บ๐—ฏ๐—ฒ๐—ฟ ๐—ผ๐—ณ ๐—”๐—ฟ๐—ด๐˜‚๐—บ๐—ฒ๐—ป๐˜๐˜€

This special variable returns the total number of arguments passed to the script.

Example

./script.sh a b c

Output: 3

Useful for input validation and argument checking.

4๏ธโƒฃ$๐Ÿฌ โ€“ ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜ ๐—ก๐—ฎ๐—บ๐—ฒ

Stores the name of the script being executed.

Useful for displaying usage information, logging, and debugging.

Example

# shell.sh (file name)

#!/bin/bash
echo "$0"

Execute: sh shell.sh
Output: shell.sh
Returns the name of the script being executed.

5๏ธโƒฃ $๐—ฃ๐—ช๐—— โ€“ ๐—ฃ๐—ฟ๐—ฒ๐˜€๐—ฒ๐—ป๐˜ ๐—ช๐—ผ๐—ฟ๐—ธ๐—ถ๐—ป๐—ด ๐——๐—ถ๐—ฟ๐—ฒ๐—ฐ๐˜๐—ผ๐—ฟ๐˜†

Returns the current working directory path where the script is running.

Example: echo "$PWD"

Output: /home/ec2-user
Useful when working with files and directories.

6๏ธโƒฃ $๐—›๐—ข๐— ๐—˜ โ€“ ๐—จ๐˜€๐—ฒ๐—ฟ'๐˜€ ๐—›๐—ผ๐—บ๐—ฒ ๐——๐—ถ๐—ฟ๐—ฒ๐—ฐ๐˜๐—ผ๐—ฟ๐˜†

Returns the home directory of the current user.

Example: echo "$HOME"

Output: /home/ec2-user

7๏ธโƒฃ$๐—จ๐—ฆ๐—˜๐—ฅ โ€“ ๐—ช๐—ต๐—ผ ๐—ถ๐˜€ ๐—ฅ๐˜‚๐—ป๐—ป๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜? (๐—–๐˜‚๐—ฟ๐—ฟ๐—ฒ๐—ป๐˜ ๐˜‚๐˜€๐—ฒ๐—ฟ)

This variable returns the username of the person running the script.

Example: echo $USER

Output: /ec2-user

8๏ธโƒฃ $$ โ€“ ๐—–๐˜‚๐—ฟ๐—ฟ๐—ฒ๐—ป๐˜ ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜ ๐—ฃ๐—œ๐——

โ€ข Returns the Process ID (PID) of the running script.

โ€ข PID (Process ID) uniquely identifies a running process in Linux.

โ€ข PIDs are commonly used for process tracking, logging, creating unique temporary files, and managing running processes.

9๏ธโƒฃ$! โ€“ ๐—Ÿ๐—ฎ๐˜€๐˜ ๐—•๐—ฎ๐—ฐ๐—ธ๐—ด๐—ฟ๐—ผ๐˜‚๐—ป๐—ฑ ๐—ฃ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€ ๐—ฃ๐—œ๐——

Returns the PID of the most recently executed background process.

Example:

When you run a command in the background using &, this variable gives you its PID.

sleep 100 & โ†’ starts the process in the background.

$! โ†’ stores the PID of the last background process (12345).

It is commonly used to monitor, track, or terminate background jobs.

If you want to stop the background process, you can use its PID with the kill command.

sleep 100 &

PID=$!

kill $PID

๐Ÿ”Ÿ $? โ€“ Exit Status of the Last Command

Stores the exit code of the last executed command.

โ€ข 0 = Success โœ…

โ€ข Non-zero = Failure โŒ

Widely used for error handling in Shell Scripts.

Example:

dnf install nginx -y

if [ $? -eq 0 ]; then

echo "Installation Successful"

else

echo "Installation Failed"

fi

โธป

For reference, I have included screenshots of both the script and its output.

Script:

Output:

๐Ÿ’ก๐—ž๐—ฒ๐˜† ๐—ง๐—ฎ๐—ธ๐—ฒ๐—ฎ๐˜„๐—ฎ๐˜†

Special variables make Shell Scripts more dynamic by helping scripts handle inputs, identify users, track processes, and respond to command execution results.