Special Variables in Shell Scripting: Small Symbols, Powerful Impact
๐จ๐ป๐ฑ๐ฒ๐ฟ๐๐๐ฎ๐ป๐ฑ๐ถ๐ป๐ด ๐ฆ๐ฝ๐ฒ๐ฐ๐ถ๐ฎ๐น ๐ฉ๐ฎ๐ฟ๐ถ๐ฎ๐ฏ๐น๐ฒ๐ ๐ถ๐ป ๐ฆ๐ต๐ฒ๐น๐น ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐๐ถ๐ป๐ด
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.



