Bash Script: built-in (or predefined) variables
In Bash, there are several built-in (or predefined) variables that hold information about the current shell session, scripts, and the environment. Some of the most common ones are listed below. While there isn't an exact number, Bash includes dozens of these variables.
Some Common Built-in Variables:
$0
: The name of the script or shell.- Example:
./
script.sh
- Example:
$1
,$2
, ...$n
: Positional parameters that represent arguments passed to the script.- Example:
./
script.sh
arg1 arg2
- Example:
$#
: The number of positional parameters (arguments) passed to the script.- Example: If
./
script.sh
arg1 arg2
, then$#
will be2
.
- Example: If
$@
: All the positional parameters (arguments) as a single string.- Example:
./
script.sh
arg1 arg2
,$@
will return"arg1 arg2"
.
- Example:
$*
: All the positional parameters as a single word.- Example:
./
script.sh
arg1 arg2
,$*
will return"arg1 arg2"
.
- Example:
$?
: The exit status of the last command.- Example: If the previous command was successful,
$?
will return0
.
- Example: If the previous command was successful,
$$
: The process ID (PID) of the current shell.- Example:
echo $$
will print the PID of the running script.
- Example:
$!
: The process ID (PID) of the last background command.- Example:
command & echo $!
will give you the PID ofcommand
run in the background.
- Example:
$-
: The current options set for the shell.- Example: If running the shell with options like
-x
(debugging),$-
will includex
.
- Example: If running the shell with options like
$_
: The last argument of the previous command.- Example: If the previous command was
echo hello world
,$_
will beworld
.
- Example: If the previous command was
Special Environment Variables:
HOME
: The current user’s home directory.- Example:
/home/user
- Example:
USER
: The current logged-in user.- Example:
user
- Example:
SHELL
: The shell being used.- Example:
/bin/bash
- Example:
PATH
: The system’s search path for executable files.- Example:
/usr/local/bin:/usr/bin:/bin
- Example:
PWD
: The current working directory.- Example:
/home/user/directory
- Example:
OLDPWD
: The previous working directory.- Example: The directory you were in before the current one.
IFS
: Internal Field Separator, used for word splitting.- Example: By default, it’s a space, tab, and newline.
LANG
: The current language/locale settings.- Example:
en_US.UTF-8
- Example:
Other Less Common Built-in Variables:
UID
: The user ID of the current user.PS1
: The primary prompt string.PS2
: The secondary prompt string.HOSTNAME
: The name of the current host.
Conclusion:
While there isn't a strict count since the number of built-in variables can vary depending on the environment, there are dozens of important ones that come pre-defined in most Bash environments. The variables listed above cover the most commonly used ones in shell scripting.
Subscribe to my newsletter
Read articles from sneh srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by