Shell special variables are predefined variables in Unix-like systems that provide valuable information about the current shell environment, command-line arguments, and the execution status of commands. These variables are essential for writing efficient and robust shell scripts.
1. $0: The Filename of the Current Script
The $0 variable holds the name of the current script. This is useful for logging or debugging purposes.
#!/bin/bash
echo "Script name: $0" 2. $n: Command-Line Arguments
The $n variables, where n is a positive integer (e.g., $1, $2, $3, etc.), represent the command-line arguments passed to the script. These are positional parameters, with $1 being the first argument, $2 the second, and so on.
#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2" 3. $#: Number of Command-Line Arguments
The $# variable returns the number of command-line arguments passed to the script.
#!/bin/bash
echo "Number of arguments: $#" 4. $@ and **$***: All Command-Line Arguments
Both $@ and $* variables represent all the command-line arguments passed to the script. However, they behave differently when enclosed in double quotes:
- $@: When quoted, it treats each argument as a separate word, preserving spaces within arguments.
- **$***: When quoted, it concatenates all arguments into a single string, with spaces between them.
#!/bin/bash
echo "Arguments using \$@:"
for arg in "$@"; do
echo "$arg"
done
echo "Arguments using \$*:"
for arg in "$*"; do
echo "$arg"
done 5. $?: Exit Status of the Last Command
The $? variable holds the exit status of the last command executed. An exit status of 0 indicates success, while any other value indicates failure.
#!/bin/bash
command
if [ $? -eq 0 ]; then
echo "Command was successful"
else
echo "Command failed"
fi 6. $$: Process ID of the Current Shell
The $$ variable returns the process ID of the current shell. For shell scripts, this is the process ID under which they are executing.
#!/bin/bash
echo "Process ID of the current shell: $$" 7. $: Process ID of the Last Background Command
The $ variable holds the process ID of the last command that was executed in the background.
#!/bin/bash
command &
echo "Process ID of the last background command: $!" 8. $-: Current Shell Options
The $- variable displays the current set of options in use by the shell.
#!/bin/bash
echo "Current shell options: $-" 9. $_: Last Argument of the Previous Command
The $_ variable holds the last argument of the previous command.
#!/bin/bash
echo "Last argument of the previous command: $_" Practical Examples
Here is a comprehensive example that demonstrates the usage of these special variables:
#!/bin/bash
echo "Script name: $0"
echo "Number of arguments: $#"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments using \$@:"
for arg in "$@"; do
echo "$arg"
done
echo "All arguments using \$*:"
for arg in "$*"; do
echo "$arg"
done
command
if [ $? -eq 0 ]; then
echo "Command was successful"
else
echo "Command failed"
fi
echo "Process ID of the current shell: $$"
command &
echo "Process ID of the last background command: $!"
echo "Current shell options: $-"
echo "Last argument of the previous command: $_" Conclusion
Shell special variables are important tools that provide essential information about the shell environment and command execution. Understanding and using these variables effectively can significantly enhance the functionality and reliability of shell scripts. By using these variables in your scripting practices, you can create more robust and efficient scripts to automate tasks and streamline workflows.