What are special variables in UNIX/Linux?
In UNIX/Linux, special variables (also known as shell variables or environment variables) are predefined variables that the shell uses to store specific information. These variables can be system-wide or user-specific and are used to control the behavior of the shell and the operating system.
Common Special Variables
Here are some of the most commonly used special variables in UNIX/Linux:
Environment Variables
-
HOME
: The home directory of the current user.echo $HOME
-
USER
: The username of the current user.echo $USER
-
PATH
: The search path for commands. This variable contains a colon-separated list of directories that the shell searches through when looking for commands.echo $PATH
-
SHELL
: The path to the current shell.echo $SHELL
-
PWD
: The present working directory.echo $PWD
-
OLDPWD
: The previous working directory.echo $OLDPWD
-
LANG
: The current locale setting, which determines the language and regional settings.echo $LANG
-
TERM
: The type of terminal to emulate when running the shell.echo $TERM
Positional Parameters
Positional parameters are variables that contain the arguments passed to a script or a shell function.
-
$0
: The name of the script or the shell.echo $0
-
$1
,$2
, ...,$N
: The first, second, ..., Nth argument passed to the script or function.echo $1 # First argument echo $2 # Second argument
-
$#
: The number of positional parameters (arguments) passed to the script or function.echo $#
-
$*
: All the positional parameters as a single word.echo $*
-
$@
: All the positional parameters as separate words.echo $@
-
$?
: The exit status of the last command executed.some_command echo $?
-
$$
: The process ID (PID) of the current shell.echo $$
-
$!
: The process ID of the last background command.some_background_command & echo $!
-
$-
: The current options set for the shell.echo $-
-
$_
: The last argument to the previous command.echo $_
Examples and Usage
Example 1: Using Positional Parameters in a Script
Here's a simple script that demonstrates the use of positional parameters:
#!/bin/bash echo "Script name: $0" echo "First argument: $1" echo "Second argument: $2" echo "Number of arguments: $#" echo "All arguments (as a single word): $*" echo "All arguments (as separate words): $@"
Run the script with arguments:
./script.sh arg1 arg2
Output:
Script name: ./script.sh
First argument: arg1
Second argument: arg2
Number of arguments: 2
All arguments (as a single word): arg1 arg2
All arguments (as separate words): arg1 arg2
Example 2: Using Environment Variables
Here's a simple script that prints some environment variables:
#!/bin/bash echo "Home Directory: $HOME" echo "Current User: $USER" echo "Current Shell: $SHELL" echo "Current Working Directory: $PWD"
Run the script:
./env_script.sh
Output:
Home Directory: /home/username
Current User: username
Current Shell: /bin/bash
Current Working Directory: /home/username
Conclusion
Special variables in UNIX/Linux are essential for shell scripting and system administration. They provide valuable information about the environment, the shell, and the state of the system. Understanding and using these variables effectively can help you write more powerful and flexible scripts.
GET YOUR FREE
Coding Questions Catalog