What are special variables in UNIX/Linux?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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

  1. HOME: The home directory of the current user.

    echo $HOME
  2. USER: The username of the current user.

    echo $USER
  3. 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
  4. SHELL: The path to the current shell.

    echo $SHELL
  5. PWD: The present working directory.

    echo $PWD
  6. OLDPWD: The previous working directory.

    echo $OLDPWD
  7. LANG: The current locale setting, which determines the language and regional settings.

    echo $LANG
  8. 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.

  1. $0: The name of the script or the shell.

    echo $0
  2. $1, $2, ..., $N: The first, second, ..., Nth argument passed to the script or function.

    echo $1 # First argument echo $2 # Second argument
  3. $#: The number of positional parameters (arguments) passed to the script or function.

    echo $#
  4. $*: All the positional parameters as a single word.

    echo $*
  5. $@: All the positional parameters as separate words.

    echo $@
  6. $?: The exit status of the last command executed.

    some_command echo $?
  7. $$: The process ID (PID) of the current shell.

    echo $$
  8. $!: The process ID of the last background command.

    some_background_command & echo $!
  9. $-: The current options set for the shell.

    echo $-
  10. $_: 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.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the success rate of the Meta interview?
How are behavioral interviews scored?
How to get selected in PayPal?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.