Skip to content

Default shell variables value

Default shell variables in Linux are predefined variables set by the shell. These variables hold essential information about the environment and system. They are accessible globally and provide critical functionality to the shell environment. Below is a detailed discussion of default shell variables and their values:


1. Environment Variables

These variables define the shell’s environment and are inherited by all child processes. Some of the most commonly used default variables include:

a) $HOME

  • Purpose: Holds the current user’s home directory path.
  • Example:

echo $HOME

# Output: /home/username

b) $USER

  • Purpose: Stores the username of the currently logged-in user.
  • Example:

echo $USER

# Output: john

c) $PATH

  • Purpose: Contains a colon-separated list of directories where the shell looks for executable files.
  • Default Value: Typically includes /bin, /usr/bin, /usr/local/bin, etc.
  • Example:

echo $PATH

# Output: /usr/local/bin:/usr/bin:/bin

d) $PWD

  • Purpose: Displays the current working directory.
  • Example:

echo $PWD

# Output: /home/username/projects

e) $SHELL

  • Purpose: Specifies the path of the default shell.
  • Example:

echo $SHELL

# Output: /bin/bash


2. Special Variables

The shell provides several built-in special variables used to manage scripts and commands. These include:

a) $?

  • Purpose: Holds the exit status of the last executed command.
  • Default Value: 0 for success; non-zero for failure.
  • Example:

ls /nonexistent

echo $?

# Output: 2 (error code for “no such file or directory”)

b) $#

  • Purpose: Indicates the number of arguments passed to a shell script.
  • Example:

./script.sh arg1 arg2

echo $#

# Output: 2

c) $@

  • Purpose: Represents all arguments passed to a script as separate words.
  • Example:

./script.sh apple banana

echo $@

# Output: apple banana

d) $*

  • Purpose: Represents all arguments passed to a script as a single word.
  • Example:

./script.sh apple banana

echo $*

# Output: apple banana

e) $0

  • Purpose: Stores the name of the script or shell program.
  • Example:

./myscript.sh

echo $0

# Output: ./myscript.sh

f) $1, $2, …

  • Purpose: Represent positional parameters, i.e., the arguments passed to a script.
  • Example:

./script.sh John 30

echo $1

# Output: John

echo $2

# Output: 30


3. Configuration Variables

a) $IFS (Internal Field Separator)

  • Purpose: Defines the character(s) used for word splitting.
  • Default Value: Space, tab, newline.
  • Example:

echo $IFS

# Output: (invisible space, tab, newline)

b) $PS1

  • Purpose: Defines the primary shell prompt.
  • Default Value: \u@\h:\w\$ (e.g., user@host:/current/directory$ ).
  • Example:

echo $PS1


4. Temporary Variables

These are not default variables but are created and used temporarily in the shell. Examples include:

  • $TMPDIR: Directory for temporary files (default: /tmp).
  • $OLDPWD: Stores the previous working directory.

5. Modifying Default Variables

You can modify the default values of shell variables, but changes usually apply only to the current session unless added to configuration files like .bashrc or .profile.

  • Example: Modifying $PATH:

export PATH=$PATH:/new/directory


6. Best Practices

  • Be cautious when modifying default shell variables to avoid breaking the shell environment.
  • Use export to propagate changes to child processes.
  • Check the current value of a variable before modifying it:

echo $PATH


Summary

Default shell variables are integral to managing the shell environment and scripting processes. They provide access to critical system and session information, enabling scripts to be more dynamic and interactive. By understanding and effectively using these variables, you can enhance the efficiency of your shell scripting tasks.