Special Variables

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.

Good Practice for Naming

Normally, these special variables are exported variables. In the common practice:

  1. We usually name all these exported global variables with 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.
  2. Ask yourself is it really necessary to use such globally exported variables.
  3. Use the naming best practice convention, like: PROGRAM_NAME to minimize impact.
  4. Before we create such variable knowingly, a simple Google search can scan the internet for known deployment.
  5. Test the variable in a controlled environment. If it doesn't break anything, you're okay to use.
  6. Unset your variable after use.


#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.

BASH_ENV

Type

String

Description

It is the bash environment origin path. It is set by automated configurations by the operating system.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it.

BASHOPTS

Type

String

Description

It is the bash options, new to BASH version 4.1. It is set by shopt command.

What You Can Do

  1. Read it
  2. Strictly Update or Delete options using shopt command.

What You Can't Do

  1. Create, Update or Delete it inside the script.

SHELLOPTS

Type

String

Description

It is the shell options, serving similar functionalities as BASHOPTS. It is set by shopt command.

What You Can Do

  1. Read it
  2. Update or Delete options using shopt command.

What You Can't Do

  1. Create, Update or Delete it inside the script.

BASH_COMMAND

Type

String

Description

Tells the current executing command. Rarely use.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

BASH_SOURCE

Type

Array

Description

Tells the trace location of the script. The first is the latest. Useful for debugging purposes and execution tracing.

What You Can Do

  1. Read

What You Can't Do

  1. Create, Update or Delete

FUNCNAME

Type

An array

Description

Tells trances of function name. The first is the latest entry. Useful for debugging purposes and execution tracing.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

BASH_LINENO

Type

An array

Description

Tells the line number of the code that executed the function. The first is the latest. Useful for debugging purposes and execution tracing.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

LINENO

Type

integer

Description

Tells the line number for current line of execution. Useful for debugging purposes and execution tracing.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

SHELL

Type

String

Description

Indicate the type of shell. It is not always defined.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

HOSTNAME

Type

String

Description

Tells the current machine's network hostname. This is similar to the output of $ uname -n.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

HOSTTYPE

Type

String

Description

Tells the current machine's machine type. like x86_64. This is similar to the output of $ uname -m.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

PWD

Type

String

Description

Tells the current directory. This is similar to the output of $ pwd.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

OLDPWD

Type

String

Description

Tells the previous directory.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

PIPESTATUS

Type

Array

Description

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
$

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

?

Type

Integer

Description

Return value of the last command. 0 indicates no error. Anything else is user-defined error.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

TIMEFORMAT

Type

String

Description

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


What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

RANDOM

Type

Integer

Description

Return non-crypto random value from 0 to 32767. Do not use it for cryptography.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

REPLY

Type

String

Description

Default variable for read command if no variable specified.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

*

Type

String

Description

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.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

@

Type

Array

Description

Expands all arguments into positional arguments.

What You Can Do

  1. Read it

What You Can't Do

  1. Create, Update or Delete it inside the script.

SECONDS

Type

Integer

Description

The actual time (wall clock) for the current bash execution.

What You Can Do

  1. Read, Update

What You Can't Do

  1. Create, Delete it inside the script.

BASH_XTRACEFD

Type

Array

Description

Variable that holds the execution traces. Use in conjunction with -x argument or (set -x).

What You Can Do

  1. Read

What You Can't Do

  1. Create, Update, Delete

GLOBIGNORE

Type

String

Description

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.

What You Can Do

  1. Create, Read, Update, Delete

What You Can't Do

  1. -

HOME

Type

String

Description

Location of the user home directory, similar to ~.

What You Can Do

  1. Read

What You Can't Do

  1. Create, Update, Delete

IFS

Type

String

Description

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.

What You Can Do

  1. Create, Read, Update, Delete

What You Can't Do

  1. -

PATH

Type

String

Description

The list of directories to terminal to search for executables and commands. The paths are separated by colon.


What You Can Do

  • Read path(s)
  • Update path(s)

To add more path, simply set it with colon:

PATH="${PATH}:/path/to/your/directory1:/path/to/your/directory2"
  • Delete path(s)

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.


What You Can't Do

  1. Create the variable (created automatically)
  2. Delete the variable values completely

XDG_PATH_DIRS

Type

String

Description

The list of directories to desktop manager to search for application launchers.

What You Can Do

  • Read path(s)
  • Update path(s)

To add more path, simply set it with colon:

XDG_PATH_DIRS="${XDG_PATH_DIRS}:/path/to/your/directory1:/path/to/your/directory2"
  • Delete path(s)

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.


What You Can't Do

  1. Create the variable (created automatically)
  2. Delete the variable values completely

TMOUT

Type

Integer

Description

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.

What You Can Do

  1. Create, Read, Update

What You Can't Do

  1. Delete

TMPDIR

Type

String

Description

The directory for temporary usage. The default is /tmp.

What You Can Do

  1. Create, Read, Update

What You Can't Do

  1. Delete - set it back to /tmp instead.

UID

Type

Integer

Description

Shell launches user ID. Set automatically.

What You Can Do

  1. Read

What You Can't Do

  1. Create, Update, Delete

USER

Type

String

Description

States the user who executes the shell. Useful for checking ROOT or ID a person.

What You Can Do

  1. Read

What You Can't Do

  1. Create, Update, Delete

GROUPS

Type

Array

Description

User permission / groups for the $USER.

What You Can Do

  1. Read

What You Can't Do

  1. Create, Update, Delete

PS1, PS2, PS3, PS4

Type

String

Description

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:

  1. PS1 - Immediate prompt. Default depends on OS. Usually, they generate something like: name@machine$ .
  2. PS2 - Continuation prompt. Default is > .
  3. PS3 - Selection prompt for select. The default is #? .
  4. PS4 - XTRACE prefix output used by "set -x". The default is ++ .

Some examples:

  • PS1
holloway:~$ echo "hello world"
hello world
holloway:~$ PS1="hello_me $ "
hello_me $ echo "hello world"
hello world
hello_me $ 


  • PS2
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:~$ 


  • PS3

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$


  • PS4

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!'


What You Can Do

  1. Read, Update

What You Can't Do

  1. Create, Delete

PROMPT_COMMAND

Type

String

Description

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.

What You Can Do

  1. Create, Read, Update, Delete

What You Can't Do

  1. -