Remember that in the general variable section, we mentioned about special variables? Well, this section describes most of them. Special variables means they are used for specific functions and you should manipulate them per their specifications. Here, we list out what are they, what actions are available, and how can you manipulate them.
Normally, these special variables are exported variables. In the common practice:
UPCASE_AND_UNDERSCORE
characters to indicate it is an exported variable. Hence, it is okay and always safe to use lowercase_and_underscore
exported variable.PROGRAM_NAME
to minimize impact.#2 is a very important consideration because for a simple bash script, you don't need to export a global variable unless you're writing a system program. It's a good thinking spot before coding your script.
String
It is the bash environment origin path. It is set by automated configurations by the operating system.
String
It is the bash options, new to BASH version 4.1. It is set by shopt
command.
shopt
command.String
It is the shell options, serving similar functionalities as BASHOPTS
. It is set by shopt
command.
shopt
command.String
Tells the current executing command. Rarely use.
Array
Tells the trace location of the script. The first is the latest. Useful for debugging purposes and execution tracing.
An array
Tells trances of function name. The first is the latest entry. Useful for debugging purposes and execution tracing.
An array
Tells the line number of the code that executed the function. The first is the latest. Useful for debugging purposes and execution tracing.
integer
Tells the line number for current line of execution. Useful for debugging purposes and execution tracing.
String
Indicate the type of shell. It is not always defined.
String
Tells the current machine's network hostname. This is similar to the output of $ uname -n
.
String
Tells the current machine's machine type. like x86_64
. This is similar to the output of $ uname -m
.
String
Tells the current directory. This is similar to the output of $ pwd
.
String
Tells the previous directory.
Array
List the return values of each last pipeline commands. The first is the initial command. You must read the array all at once. Example:
$ echo "hello" | grep "hell" | grep "e"
hello
$ echo "${PIPESTATUS[@]}"
0 0 0
$
Integer
Return value of the last command. 0 indicates no error. Anything else is user-defined error.
String
Set the reporting format for time
command. Useful for debugging and performance measurement. Example:
TIMEFORMAT="
Benchmark
---------
CPU: %P%% (%3Ss kernel) (%3Us user)
"
and apply time
to ls
yields:
$ time ls
bin Desktop Downloads Pictures Public src Videos
code_development Documents Music pkg snap Templates
Benchmark
---------
CPU: 0.00% (0.000s kernel) (0.000s user)
$
Refer to manual $ man time
for all the formats. Exmaple link: https://manpages.debian.org/stretch/time/time.1.en.html
Integer
Return non-crypto random value from 0 to 32767. Do not use it for cryptography.
String
Default variable for read
command if no variable specified.
String
Expands all arguments into a single string joined by $IFS
variable. If $IFS
is empty, then all arguments are joined by space. If $IFS
is unset, all arguments are joined without any separator characters.
Array
Expands all arguments into positional arguments.
Integer
The actual time (wall clock) for the current bash execution.
Array
Variable that holds the execution traces. Use in conjunction with -x
argument or (set -x
).
String
Have the terminal and bash system to ignore a pattern. Using :
to separate multiple patterns. Example, setting:
*.php
- ignores all .php files.a*
- ignores all files starts with a.a*:*.php
- ingores all the above.String
Location of the user home directory, similar to ~
.
String
The pattern to interpret a word/line split. The default is <space><tab><newline>
. You can alter this variable to match the separation pattern you want (e.g. colon) before executing your command. Best practice for using it is:
old_IFS="$IFS" && IFS="your pattern"
... # execute your command
IFS="$old_IFS" && unset old_IFS
The first line is to backup existing IFS pattern before setting yours. After your usage, the IFS is restored from the backup.
String
The list of directories to terminal to search for executables and commands. The paths are separated by colon.
To add more path, simply set it with colon:
PATH="${PATH}:/path/to/your/directory1:/path/to/your/directory2"
To delete a path from the list, use the parameter expansion / string manipulation method.
PATH="${PATH/%:\/path\/wrong\/dir\/pattern}"
NOTE: it is necessary to use forward slash cancelling the % search and the backslash.
String
The list of directories to desktop manager to search for application launchers.
To add more path, simply set it with colon:
XDG_PATH_DIRS="${XDG_PATH_DIRS}:/path/to/your/directory1:/path/to/your/directory2"
To delete a path from the list, use the parameter expansion / string manipulation method.
XDG_PATH_DIRS="${XDG_PATH_DIRS/%:\/path\/wrong\/dir\/pattern}"
NOTE: it is necessary to use forward slash cancelling the % search and the backslash.
Integer
Setting a command timeout for waiting. Very useful for waiting user command to timeout and exit the program with error. If there is no command waiting, the terminal will in fact, logout upon timeout.
Set the variable to NULL or 0 will ignore this variable.
String
The directory for temporary usage. The default is /tmp
.
/tmp
instead.Integer
Shell launches user ID. Set automatically.
String
States the user who executes the shell. Useful for checking ROOT or ID a person.
Array
User permission / groups for the $USER
.
String
Prompt statement controls. It displays the prompt messages in the terminal as an indication of waiting for input or displaying appropriate data for command. There are 4 levels:
name@machine$
.>
.select
. The default is #?
.set -x
". The default is ++
.Some examples:
holloway:~$ echo "hello world"
hello world
holloway:~$ PS1="hello_me $ "
hello_me $ echo "hello world"
hello world
hello_me $
holloway:~$ echo "Hello
> World
> New
> Line
> "
Hello
World
New
Line
holloway:~$ PS2="continue> "
holloway:~$ echo "Hello
continue> World
continue> New
continue> Line
continue> "
Hello
World
New
Line
holloway:~$
script:
#!/bin/bash
run_select() {
select i in mon tue exit; do
case $i in
mon)
echo "Monday"
;;
tue)
echo "Tuesday"
;;
exit)
break
;;
esac
done
}
echo "with current PS3: $PS3"
run_select
echo ""
PS3="Select options (1-3)>"
echo "now select with current PS3: $PS3"
run_select
Running the script yields:
holloway:Desktop$ ./demo.sh
with current PS3:
1) mon
2) tue
3) exit
#? 1
Monday
#? 3
now select with current PS3: Select options (1-3)>
1) mon
2) tue
3) exit
Select options (1-3)>1
Monday
Select options (1-3)>3
holloway:Desktop$
Script:
#!/bin/bash
shout() {
echo "shout out to the world!"
echo "we did it!"
}
echo "Hello World!"
shout
Execution:
holloway:Desktop$ bash -x demo.sh 2> xlog
Hello World!
shout out to the world!
we did it!
holloway:Desktop$ cat xlog
+ echo 'Hello World!'
+ shout
+ echo 'shout out to the world!'
+ echo 'we did it!'
holloway:Desktop$ export PS4='${BASH_SOURCE[0]}|${BASH_LINENO[0]}|${LINENO}|${FUNCNAME[0]} ++ '
holloway:Desktop$ bash -x demo.sh 2> xlog
Hello World!
shout out to the world!
we did it!
holloway:Desktop$ cat xlog
demo.sh|0|8|main ++ echo 'Hello World!'
demo.sh|0|9|main ++ shout
demo.sh|9|4|shout ++ echo 'shout out to the world!'
demo.sh|9|5|shout ++ echo 'we did it!'
String
Run the given command before prompt. Use alongside with PS1 prompt. Example:
holloway:Desktop$ PROMPT_COMMAND='date +%k:%m:%S'
16:10:08
holloway:Desktop$ echo "hello world"
hello world
16:10:10
holloway:Desktop$ echo "hello world"
hello world
16:10:12
holloway:Desktop$ unset PROMPT_COMMAND
holloway:Desktop$ echo "hello world"
hello world
holloway:Desktop$ echo "hello world"
hello world
holloway:Desktop$
NOTE: This is not a extended PS1 prompt feature. It is meant for subroutine per prompt. If you need the output as a prompt, set it inside PS1 instead.