Skip to content

Latest commit

 

History

History
142 lines (118 loc) · 3.96 KB

File metadata and controls

142 lines (118 loc) · 3.96 KB

In Linux, several special variables (also called shell variables or environment variables) are commonly used when working with the command line and scripting. Here are the most important ones you should know:


1. Positional Parameters (Command-line Arguments)

Variable Description
$0 The name of the script or command being executed.
$1, $2, ..., $9 The first, second, ..., ninth argument passed to the script.
${10}, ${11}, ... Arguments beyond the ninth (must be enclosed in {}).
$* All arguments passed to the script as a single string.
$@ All arguments passed to the script as separate strings.
$# The number of arguments passed to the script.

Example: Positional Parameters in a Script

#!/bin/bash
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "All Arguments (\$*): $*"
echo "All Arguments (\$@): $@"
echo "Total Arguments: $#"

Run the script:

./script.sh Hello World

Output:

Script Name: ./script.sh
First Argument: Hello
Second Argument: World
All Arguments ($*): Hello World
All Arguments ($@): Hello World
Total Arguments: 2

2. Exit Status and Process Control

Variable Description
$? Exit status of the last executed command.
$$ Process ID (PID) of the current script or shell.
$! Process ID of the last background job.
$- Current shell options (set using set -o).

Example: Check PID

echo "Current Process ID: $$"
sleep 10 &
echo "Background Process ID: $!"

3. Shell Environment Variables

Variable Description
$HOME User's home directory (/home/username).
$USER Current logged-in username.
$PATH Directories where executables are searched for.
$SHELL Default shell (/bin/bash, /bin/zsh, etc.).
$PWD Current working directory.
$OLDPWD Previous working directory.
$EDITOR Default text editor (vim, nano, etc.).
$LANG System language settings.

Example: Display User Information

echo "User: $USER"
echo "Home Directory: $HOME"
echo "Current Shell: $SHELL"

4. Process Management

Variable Description
$PPID Parent process ID (PID of the parent shell).
$UID User ID of the current user.
$EUID Effective user ID (useful for checking root access).

Example: Check User and Parent Process

echo "User ID: $UID"
echo "Effective User ID: $EUID"
echo "Parent Process ID: $PPID"

5. Random and Time-related Variables

Variable Description
$RANDOM Generates a random number (0 - 32767).
$SECONDS Number of seconds since the script started.
$LINENO Current line number in a script.

Example: Generate Random Number

echo "Random Number: $RANDOM"
echo "Script Running Time: $SECONDS seconds"

6. Input/Output Variables

Variable Description
$IFS Internal Field Separator (default: space, tab, newline).
$PS1 Primary shell prompt string ($ for normal users, # for root).
$PS2 Secondary shell prompt (> used in multi-line commands).

Example: Change Shell Prompt

export PS1="MyShell> "

7. Special Variables for Debugging

Variable Description
$_ Last argument of the previous command.
$BASH_SOURCE Script filename.
$FUNCNAME Current function name (inside a function).

Example: Reuse Last Argument

echo "Hello World"
echo $_   # Output: World

Conclusion

These special variables help in scripting, automation, and debugging. You should experiment with them in scripts to understand their behavior better.

Would you like specific examples for any of these? 🚀