Wait—there are hidden variables and reserved words? Yes! There are words you can't use in your script unless properly contained in a construct such as a string. Global variables are available in a global context, which means that they are visible to all scripts in the current shell or open shell consoles. In a later chapter, we will explore global shell variables more, but just so you're aware, know that there are useful variables available for you to reuse, such as $USER
, $PWD
, $OLDPWD
, and $PATH
.
To see a list of all shell environment variables, you can use the env
command (the output has been cut short):
$ env XDG_VTNR=7 XDG_SESSION_ID=c2 CLUTTER_IM_MODULE=xim XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/rbrash SESSION=ubuntu SHELL=/bin/bash TERM=xterm-256color XDG_MENU_PREFIX=gnome- VTE_VERSION=4205 QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1 WINDOWID=81788934 UPSTART_SESSION=unix:abstract=/com/ubuntu/upstart-session/1000/1598 GNOME_KEYRING_CONTROL= GTK_MODULES=gail:atk-bridge:unity-gtk-module USER=rbrash ....
Note
Modifying the PATH
environment variable can be very useful. It can also be frustrating, because it contains the filesystem path to binaries. For example, you have binaries in /bin
or /sbin
or /usr/bin
, but when you run a single command, the command is run without you specifying the path.
Alright, so we have acknowledged the existence of pre-existing variables and that there could be new global variables created by the user or other programs. When using variables that have a high probability of being similarly named, be careful to make them specific to your application.
In addition to hidden variables, there are also words that are reserved for use within a script or shell. For example, if and else are words that are used to provide conditional logic to scripts. Imagine if you created a command, variable, or function (more later on this) with the same name as one that already exists? The script would likely break or run an erroneous operation.
Note
When trying to avoid any naming collisions (or namespace collisions), try to make your variables more likely to be used by your application by appending or prefixing an identifier that is likely to be unique.
The following list contains some of the more common reserved words that you will encounter. Some of which are likely to look very familiar because they tell the Bash interpreter to interpret any text in a specific way, redirect output, run an application in the background, or are even used in other programming/scripting languages.
if
,elif
,else
,fi
while
,do
,for
,done
,continue
,break
case
,select
,time
function
&
,|
,>
,<
,!
,=
#
,$
,(, )
,;
,{, }
,[, ]
,\
Note
For the full reference, go to: https://www.gnu.org/software/bash/manual/html_node/Reserved-Word-Index.html.
The last element in the list contains an array of specific characters that tell Bash to perform specific functionalities. The pound sign signifies a comment for example. However, the backslash \
is very special because it is an escape character. Escape characters are used to escape or stop the interpreter from executing specific functionality when it sees those particular characters. For example:
$ echo # Comment $ echo \# Comment # Comment
Escaping characters will become very useful in Chapter 2, Acting like a Typewriter and File Explorer, when working with strings and single/double quotes.