Linux Professional Institute Learning Logo.
Skip to main content
  • Home
    • All Resources
    • LPI Learning Materials
    • Become a Contributor
    • Publishing Partners
    • Become a Publishing Partner
    • About
    • FAQ
    • Contributors
    • Contact
  • LPI.org
105.1 Lesson 2
Topic 105: Shells and Shell Scripting
105.1 Customize and use the shell environment
  • 105.1 Lesson 1
  • 105.1 Lesson 2
  • 105.1 Lesson 3
105.2 Customize or write simple scripts
  • 105.2 Lesson 1
  • 105.2 Lesson 2
Topic 106: User Interfaces and Desktops
106.1 Install and configure X11
  • 106.1 Lesson 1
106.2 Graphical Desktops
  • 106.2 Lesson 1
106.3 Accessibility
  • 106.3 Lesson 1
Topic 107: Administrative Tasks
107.1 Manage user and group accounts and related system files
  • 107.1 Lesson 1
  • 107.1 Lesson 2
107.2 Automate system administration tasks by scheduling jobs
  • 107.2 Lesson 1
  • 107.2 Lesson 2
107.3 Localisation and internationalisation
  • 107.3 Lesson 1
Topic 108: Essential System Services
108.1 Maintain system time
  • 108.1 Lesson 1
  • 108.1 Lesson 2
108.2 System logging
  • 108.2 Lesson 1
  • 108.2 Lesson 2
108.3 Mail Transfer Agent (MTA) basics
  • 108.3 Lesson 1
108.4 Manage printers and printing
  • 108.4 Lesson 1
Topic 109: Networking Fundamentals
109.1 Fundamentals of internet protocols
  • 109.1 Lesson 1
  • 109.1 Lesson 2
109.2 Persistent network configuration
  • 109.2 Lesson 1
  • 109.2 Lesson 2
109.3 Basic network troubleshooting
  • 109.3 Lesson 1
  • 109.3 Lesson 2
109.4 Configure client side DNS
  • 109.4 Lesson 1
Topic 110: Security
110.1 Perform security administration tasks
  • 110.1 Lesson 1
110.2 Setup host security
  • 110.2 Lesson 1
110.3 Securing data with encryption
  • 110.3 Lesson 1
  • 110.3 Lesson 2
  1. Topic 105: Shells and Shell Scripting
  2. 105.1 Customize and use the shell environment
  3. 105.1 Lesson 2

105.1 Lesson 2

Certificate:

LPIC-1

Version:

5.0

Topic:

105 Shells and Shell Scripting

Objective:

105.1 Customize and use the shell environment

Lesson:

2 of 3

Introduction

Think of a variable as an imaginary box in which to temporarily place a piece of information. The same as with its initialization scripts, Bash classifies variables as either shell/local (those which live only within the limits of the shell in which they were created) or environment/global (those that are inherited by children shells and/or processes). In fact, in the previous lesson we had a look at shells and their configuration or initialization scripts. It is now convenient to point out that the power of these startup files lies in the fact that they allow us to use variables — as well as aliases and functions — which help us create and customize the shell environment of our choice.

Variables: Assignment and Reference

A variable can be defined as a name containing a value.

In Bash, giving a value to a name is called variable assignment and is the way in which we create or set variables. On the other hand, the process of accessing the value contained in the name is called variable referencing.

The syntax for assigning variables is:

<variable_name>=<variable_value>

For example:

$ distro=zorinos

The variable distro equals zorinos, that is to say, there is a portion of memory holding the value zorinos — with distro being the pointer to it.

Note, however, that there can not be any space on either side of the equal sign when assigning a variable:

$ distro =zorinos
-bash: distro: command not found
$ distro= zorinos
-bash: zorinos: command not found

Because of our mistake, Bash read distro and zorinos as commands.

To reference a variable (that is, to check its value) we use the echo command preceding the variable name with a $ sign:

$ echo $distro
zorinos

Variable Names

When choosing the name of variables, there are certain rules that we must take into consideration.

The name of a variable may contain letters (a-z,A-Z), numbers (0-9) and underscores (_):

$ distro=zorinos
$ echo $distro
zorinos
$ DISTRO=zorinos
$ echo $DISTRO
zorinos
$ distro_1=zorinos
$ echo $distro_1
zorinos
$ _distro=zorinos
$ echo $_distro
zorinos

It may not start with a number or Bash will get confused:

$ 1distro=zorinos
-bash: 1distro=zorinos: command not found

It may not contain spaces (not even using quotes); by convention, underscores are used instead:

$ "my distro"=zorinos
-bash: my: command not found
$ my_distro=zorinos
$ echo $my_distro
zorinos

Variable Values

Concerning the reference or value of variables it is also important to consider a number of rules.

Variables may contain any alphanumerical characters (a-z,A-Z,0-9) as well as most other characters (?,!,*,.,/, etc.):

$ distro=zorin12.4?
$ echo $distro
zorin12.4?

Variable values must be enclosed in quotes if they contain single spaces:

$ distro=zorin 12.4
-bash: 12.4: command not found
$ distro="zorin 12.4"
$ echo $distro
zorin 12.4
$ distro='zorin 12.4'
$ echo $distro
zorin 12.4

Variable values must also be enclosed in quotes if they contain such characters as those used for redirection (<,>) or the pipe symbol (|). The only thing the following command does is create an empty file named zorin:

$ distro=>zorin
$ echo $distro

$ ls zorin
zorin

This works, though, when we use the quotes:

$ distro=">zorin"
$ echo $distro
>zorin

However, single and double quotes are not always interchangeable. Depending on what we are doing with a variable (assigning or referencing), the use of one or the other has implications and will yield different results. In the context of variable assignment single quotes take all the characters of the variable value literally, whereas double quotes allow for variable substitution:

$ lizard=uromastyx
$ animal='My $lizard'
$ echo $animal
My $lizard
$ animal="My $lizard"
$ echo $animal
My uromastyx

On the other hand, when referencing a variable whose value includes some initial (or extra) spaces — sometimes combined with asterisks — it is mandatory that we use double quotes after the echo command to avoid field splitting and pathname expansion:

$ lizard="   genus   |   uromastyx"
$ echo $lizard
genus | uromastyx
$ echo "$lizard"
   genus   |   uromastyx

If the reference of the variable contains a closing exclamation mark, this must be the last character in the string (as otherwise Bash will think we are referring to a history event):

$ distro=zorin.?/!os
-bash: !os: event not found
$ distro=zorin.?/!
$ echo $distro
zorin.?/!

Any backslashes must be escaped with another backslash. Also, if a backslash is the last character in the string and we do not escape it Bash will interpret we want a line break and give us a new line:

$ distro=zorinos\
>
$ distro=zorinos\\
$ echo $distro
zorinos\

In the next two sections we will sum up the main differences between local and environment variables.

Local or Shell Variables

Local or shell variables exist only in the shell in which they are created. By convention, local variables are written in lower-case letters.

For the sake of carrying out a few tests, let us create a local variable. As explained above, we choose an appropriate variable name and equate it to an appropriate value. For instance:

$ reptile=tortoise

Let us now use the echo command to reference our variable and check that everything went as expected:

$ echo $reptile
tortoise

In certain scenarios — such as when writing scripts — immutability can be an interesting feature of variables. If we want our variables to be immutable, we can either create them readonly:

$ readonly reptile=tortoise

Or turn them so after they have been created:

$ reptile=tortoise
$ readonly reptile

Now, if we try to change the value of reptile, Bash will refuse:

$ reptile=lizard
-bash: distro: readonly variable
Note

To list all readonly variables in our current session, type readonly or readonly -p into the terminal.

A useful command when dealing with local variables is set.

set outputs all of the currently assigned shell variables and functions. Since that can be a lot of lines (try it yourself!), it is recommended to use it in combination with a pager such as less:

$ set | less
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="4" [2]="12" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='4.4.12(1)-release'
(...)

Is our reptile variable there?

$ set | grep reptile
reptile=tortoise

Yes, there it is!

However, reptile — being a local variable — will not be inherited by any child processes spawned from the current shell:

$ bash
$ set | grep reptile
$

And, of course, we cannot echo out its value either:

$ echo $reptile
$
Note

By typing the bash command into the terminal we open a new (child) shell.

To unset any variables (either local or global), we use the unset command:

$ echo $reptile
tortoise
$ unset reptile
$ echo $reptile
$
Note

unset must be followed by the name of the variable alone (not preceded by the $ symbol).

Global or Environment Variables

Global or environment variables exist for the current shell as well as for all subsequent processes spawned from it. By convention, environment variables are written in uppercase letters:

$ echo $SHELL
/bin/bash

We can recursively pass the value of these variables to other variables and the value of the latter will ultimately expand to that of the former:

$ my_shell=$SHELL
$ echo $my_shell
/bin/bash
$ your_shell=$my_shell
$ echo $your_shell
/bin/bash
$ our_shell=$your_shell
$ echo $our_shell
/bin/bash

In order for a local shell variable to become an environment variable, the export command must be used:

$ export reptile

With export reptile we have turned our local variable into an environment variable so that child shells can recognize it and use it:

$ bash
$ echo $reptile
tortoise

Likewise, export can be used to set and export a variable, all at once:

$ export amphibian=frog

Now we can open a new instance of Bash and successfully reference the new variable:

$ bash
$ echo $amphibian
frog
Note

With export -n <VARIABLE-NAME> the variable will be turned back into a local shell variable.

The export command will also give us a list of all existing environment variables when typed on its own (or with the -p option):

$ export
declare -x HOME="/home/user2"
declare -x LANG="en_GB.UTF-8"
declare -x LOGNAME="user2"
(...)
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
declare -x PWD="/home/user2"
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_CLIENT="192.168.1.10 49330 22"
declare -x SSH_CONNECTION="192.168.1.10 49330 192.168.1.7 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm-256color"
declare -x USER="user2"
declare -x XDG_RUNTIME_DIR="/run/user/1001"
declare -x XDG_SESSION_ID="8"
declare -x reptile="tortoise"
Note

The command declare -x is equivalent to export.

Two more commands that can be used to print a list of all environment variables are env and printenv:

$ env
SSH_CONNECTION=192.168.1.10 48678 192.168.1.7 22
LANG=en_GB.UTF-8
XDG_SESSION_ID=3
USER=user2
PWD=/home/user2
HOME=/home/user2
SSH_CLIENT=192.168.1.10 48678 22
SSH_TTY=/dev/pts/0
MAIL=/var/mail/user2
TERM=xterm-256color
SHELL=/bin/bash
SHLVL=1
LOGNAME=user2
XDG_RUNTIME_DIR=/run/user/1001
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
_=/usr/bin/env

On top of being a synonym for env, we can sometimes use printenv in a similar way as we use the echo command to check for the value of a variable:

$ echo $PWD
/home/user2
$ printenv PWD
/home/user2

Note, however, that with printenv the variable name is not preceded by $.

Note

PWD stores the path of the present working directory. We will learn about this and other common environment variables later.

Running a Program in a Modified Environment

env can be used to modify the shell environment at a program’s time of execution.

To start a new Bash session with as empty an environment as possible — clearing most variables (as well as functions and aliases) — we will use env with the -i option:

$ env -i bash

Now most of our environment variables are gone:

$ echo $USER
$

And only a few remain:

$ env
LS_COLORS=
PWD=/home/user2
SHLVL=1
_=/usr/bin/printenv

We can also use env to set a particular variable for a particular program.

In our previous lesson, when discussing non-interactive non-login shells, we saw how scripts do not read any standard startup files but instead they look for the value of the BASH_ENV variable and use it as their startup file if it exists.

Let us demonstrate this process:

  1. We create our own startup file called .startup_script with the following content:

    CROCODILIAN=caiman
  2. We write a Bash script named test_env.sh with the following content:

    #!/bin/bash
    
    echo $CROCODILIAN
  3. We set the executable bit for our test_env.sh script:

    $ chmod +x test_env.sh
  4. Finally, we use env to set BASH_ENV to .startup_script for test_env.sh:

    $ env BASH_ENV=/home/user2/.startup_script ./test_env.sh
    caiman

    The env command is implicit even if we get rid of it:

    $ BASH_ENV=/home/user2/.startup_script ./test_env.sh
    caiman
Note

If you do not understand the line #!/bin/bash or the chmod +x command, do not panic! We will learn everything necessary about shell scripting in future lessons. For now, just remember that to execute a script from within its own directory we use ./some_script.

Common Environment Variables

Time now for reviewing some of the most relevant environment variables that are set in Bash configuration files.

DISPLAY

Related to the X server, this variable’s value is normally made up of three elements:

  • The hostname (the absence of it means localhost) where the X server is running.

  • A colon as a delimiter.

  • A number (it is normally 0 and refers to the computer’s display).

    $ printenv DISPLAY
    :0

    An empty value for this variable means a server without an X Window System. An extra number — as in my.xserver:0:1 — would refer to the screen number if more than one exists.

HISTCONTROL

This variable controls what commands get saved into the HISTFILE (see below). Their are three possible values:

ignorespace

Commands starting with a space will not be saved.

ignoredups

A command which is the same as the previous one will not be saved.

ignoreboth

Commands which fall into any of the two previous categories will not be saved.

$ echo $HISTCONTROL
ignoreboth
HISTSIZE

This sets the number of commands to be stored in memory while the shell session lasts.

$ echo $HISTSIZE
1000
HISTFILESIZE

This sets the number of commands to be saved in HISTFILE both at the start and at the end of the session:

$ echo $HISTFILESIZE
2000
HISTFILE

The name of the file which stores all commands as they are typed. By default this file is located at ~/.bash_history:

$ echo $HISTFILE
/home/user2/.bash_history
Note

To view the contents of HISTFILE, we simply type history. Alternatively, we can specify the number of commands we want to see by passing an argument (the number of the most recent commands) to history as in history 3.

HOME

This variable stores the absolute path of the current user’s home directory and it is set when the user logs in.

This piece of code — from ~/.profile — is self-explanatory (it sources "$HOME/.bashrc" if it exists):

    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
Note

If you do not quite understand the if statement, do not worry: just refer to the lessons about shell scripting.

Remember that ~ is equivalent to $HOME:

$ echo ~; echo $HOME
/home/carol
/home/carol
Note

Commands can be concatenated with a semicolon (;).

We can also prove this with an if statement:

$ if [ ~ == "$HOME" ]; then echo "true"; else "false"; fi
true
Note

Remember: The equals sign = is used for variable assignment. == is used to test for equality.

HOSTNAME

This variable stores the TCP/IP name of the host computer:

$ echo $HOSTNAME
debian
HOSTTYPE

This stores the architecture of the host computer’s processor:

$ echo $HOSTTYPE
x86_64
LANG

This variable saves the locale of the system:

$ echo $LANG
en_UK.UTF-8
LD_LIBRARY_PATH

This variable consists of a colon-separated set of directories where shared libraries are shared by programs:

$ echo $LD_LIBRARY_PATH
/usr/local/lib
MAIL

This variable stores the file in which Bash searches for email:

$ echo $MAIL
/var/mail/carol

Another common value for this variable is /var/spool/mail/$USER.

MAILCHECK

This variable stores a numeric value which indicates in seconds the frequency with which Bash checks for new mail:

$ echo $MAILCHECK
60
PATH

This environment variable stores the list of directories where Bash looks for executable files when told to run any program. In our example machine this variable is set through the system-wide /etc/profile file:

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

Through the if statement the identity of the user is tested and — depending on the result of the test (root or otherwise) — we will obtain one PATH or the other. Finally the chosen PATH is propagated with export.

Observe two things regarding the value of PATH:

  • Directory names are written using absolute paths.

  • The colon is used as a delimiter.

    If we wanted to include the folder /usr/local/sbin in the PATH for regular users, we will modify the line so that it looks like this:

    (...)
    else
      PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/sbin"
    fi
    export PATH

    Now we can see how the value of the variable changes when we login as a regular user:

    # su - carol
    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/sbin
    Note

    We could have also added /usr/local/sbin to the user’s PATH at the command line by typing either PATH=/usr/local/sbin:$PATH or PATH=$PATH:/usr/local/sbin — the former making /usr/local/sbin the first directory to be searched for executable files; the latter making it the last.

PS1

This variable stores the value of the Bash prompt. In the following chunk of code (also from /etc/profile), the if statement tests for the identity of the user and gives them a very discrete prompt accordingly ( # for root or $ for regular users):

if [ "`id -u`" -eq 0 ]; then
  PS1='# '
else
  PS1='$ '
fi
Note

The id of root is 0. Become root and test it yourself with id -u.

Other Prompt Variables include:

PS2

Normally set to > and used as a continuation prompt for long multiline commands.

PS3

Used as the prompt for the select command.

PS4

Normally set to + and used for debugging.

SHELL

This variable stores the absolute path of the current shell:

$ echo $SHELL
/bin/bash
USER

This stores the name of the current user:

$ echo $USER
carol

Guided Exercises

  1. Observe the variable assignment under the “Command(s)” column and indicate if the resulting variable is “Local” or “Global”:

    Command(s) Local Global

    debian=mother

    ubuntu=deb-based

    mint=ubuntu-based; export mint

    export suse=rpm-based

    zorin=ubuntu-based

  2. Study the “Command” and the “Output” and explain the meaning:

    Command Output Meaning

    echo $HISTCONTROL

    ignoreboth

    echo ~

    /home/carol

    echo $DISPLAY

    reptilium:0:2

    echo $MAILCHECK

    60

    echo $HISTFILE

    /home/carol/.bash_history

  3. Variables are being set incorrectly under the “Wrong Command” column. Provide the missing information under “Right Command” and “Variable Reference” so that we get the “Expected Output”:

    Wrong Command Right Command Variable Reference Expected Output

    lizard =chameleon

    chameleon

    cool lizard=chameleon

    chameleon

    lizard=cha|me|leon

    cha|me|leon

    lizard=/** chameleon **/

    /** chamelon **/

    win_path=C:\path\to\dir\

    C:\path\to\dir\

  4. Consider the purpose and write the appropriate command:

    Purpose Command

    Set the language of the current shell to Spanish UTF-8 (es_ES.UTF-8).

    Print the name of the current working directory.

    Reference the environment variable which stores the information about ssh connections.

    Set PATH to include /home/carol/scripts as the last directory to search for executables.

    Set the value of my_path to PATH.

    Set the value of my_path to that of PATH.

  5. Create a local variable named mammal and assign it the value gnu:

  6. Using variable substitution, create another local variable named var_sub with the appropriate value so that when referenced via echo $var_sub we obtain: The value of mammal is gnu:

  7. Turn mammal into an environment variable:

  8. Search for it with set and grep:

  9. Search for it with env and grep:

  10. Create, in two consecutive commands, an environment variable named BIRD whose value is penguin:

  11. Create, in a single command, an environment variable named NEW_BIRD whose value is yellow-eyed penguin:

  12. Assuming you are user2, create a folder named bin in your home directory:

  13. Type the command to add the ~/bin folder to your PATH so that it is the first directory bash searches for binaries:

  14. To guarantee the value of PATH remains unaltered across reboots, what piece of code — in the form of an if statement — would you put into ~/.profile?

Explorational Exercises

  1. let: more than arithmetic expression evaluation:

    • Do a manpage or web search for let and its implications when setting variables and create a new local variable named my_val whose value is 10 — as a result of adding 5 and 5:

    • Now create another variable named your_val whose value is 5 — as a result of dividing the value of my_val into 2:

  2. The result of a command in a variable? Of course, that is possible; it is called command substitution. Investigate it and study the following function named music_info:

    music_info(){
    latest_music=`ls -l1t ~/Music | head -n 6`
    echo -e "Your latest 5 music files:\n$latest_music"
    }

    The result of the command ls -l1t ~/Music | head -n 6 becomes the value of the variable latest_music. Then the variable latest_music is referenced in the echo command (which outputs the total number of bytes occupied by the Music folder and the latest five music files stored in the Music folder — one per line).

    Which of the following is a valid synonym for

    latest_music=`ls -l1t ~/Music | head -n 6`

    Option A:

    latest_music=$(ls -l1t ~/Music| head -n 6)

    Option B:

    latest_music="(ls -l1t ~/Music| head -n 6)"

    Option C:

    latest_music=((ls -l1t ~/Music| head -n 6))

Summary

In this lesson we learned:

  • Variables are a very important part of the shell environment as they are used by the shell itself as well as by other programs.

  • How to assign and reference variables.

  • The differences between local and globlal (or environment) variables.

  • How to make variables readonly.

  • How to turn a local variable into an environment variable with the export command.

  • How to list all environment variables.

  • How to run a program in a modified environment.

  • How to make variables persistent with the help of startup scripts.

  • Some common environment variables: DISPLAY, HISTCONTROL, HISTSIZE, HISTFILESIZE, HISTFILE, HOME, HOSTNAME, HOSTTYPE, LANG, LD_LIBRARY_PATH, MAIL, MAILCHECK, PATH, PS1 (and other prompt variables), SHELL and USER.

  • The meaning of the tilde (~).

  • The very basics of if statements.

Commands used in this lesson:

echo

Reference a variable.

ls

List directory contents.

readonly

Make variables immutable. List all readonly variables in current session.

set

List all variables and functions in current session.

grep

Print lines matching a pattern.

bash

Launch a new shell

unset

Unset variables.

export

Turn a local variable into an environment variable. List environment variables.

env

List environment variables. Run a program in a modified environment.

printenv

List environment variables. Reference a variable.

chmod

Change mode bits of a file, for example make it executable.

history

List previous commands.

su

Change user ID or become superuser.

id

Print user ID.

Answers to Guided Exercises

  1. Observe the variable assignment under the “Command(s)” column and indicate if the resulting variable is “Local” or “Global”:

    Command(s) Local Global

    debian=mother

    Yes

    No

    ubuntu=deb-based

    Yes

    No

    mint=ubuntu-based; export mint

    No

    Yes

    export suse=rpm-based

    No

    Yes

    zorin=ubuntu-based

    Yes

    No

  2. Study the “Command” and the “Output” and explain the meaning:

    Command Output Meaning

    echo $HISTCONTROL

    ignoreboth

    Both duplicate commands and those starting with a space will not be saved in history.

    echo ~

    /home/carol

    The HOME of carol is /home/carol.

    echo $DISPLAY

    reptilium:0:2

    reptilium machine has a X server running and we are using the second screen of the display.

    echo $MAILCHECK

    60

    Mail will be checked every minute.

    echo $HISTFILE

    /home/carol/.bash_history

    history will be saved in /home/carol/.bash_history.

  3. Variables are being set incorrectly under the “Wrong Command” column. Provide the missing information under “Right Command” and “Variable Reference” so that we get the “Expected Output”:

    Wrong Command Right Command Variable Reference Expected Output

    lizard =chameleon

    lizard=chameleon

    echo $lizard

    chameleon

    cool lizard=chameleon

    cool_lizard=chameleon (for example)

    echo $cool_lizard

    chameleon

    lizard=cha|me|leon

    lizard="cha|me|leon" or lizard='cha|me|leon'

    echo $lizard

    cha|me|leon

    lizard=/** chameleon **/

    lizard="/** chameleon **/" or lizard='/** chameleon **/'

    echo "$lizard"

    /** chamelon **/

    win_path=C:\path\to\dir\

    win_path=C:\\path\\to\\dir\\

    echo $win_path

    C:\path\to\dir\

  4. Consider the purpose and write the appropriate command:

    Purpose Command

    Set the language of the current shell to Spanish UTF-8 (es_ES.UTF-8).

    LANG=es_ES.UTF-8

    Print the name of the current working directory

    echo $PWD or pwd

    Reference the environment variable which stores the information about ssh connections

    echo $SSH_CONNECTION

    Set PATH to include /home/carol/scripts as the last directory to search for executables.

    PATH=$PATH:/home/carol/scripts

    Set the value of my_path to PATH.

    my_path=PATH

    Set the value of my_path to that of PATH.

    my_path=$PATH

  5. Create a local variable named mammal and assign it the value gnu:

    mammal=gnu
  6. Using variable substitution, create another local variable named var_sub with the appropriate value so that when referenced via echo $var_sub we obtain The value of mammal is gnu:

    var_sub="The value of mammal is $mammal"
  7. Turn mammal into an environment variable:

    export mammal
  8. Search for it with set and grep:

    set | grep mammal
  9. Search for it with env and grep:

    env | grep mammal
  10. Create, in two consecutive commands, an environment variable named BIRD whose value is penguin:

    BIRD=penguin; export BIRD
  11. Create, in a single command, an environment variable named NEW_BIRD whose value is yellow-eyed penguin:

    export NEW_BIRD="yellow-eyed penguin"

    or

    export NEW_BIRD='yellow-eyed penguin'
  12. Assuming you are user2, use mkdir to create a folder named bin in your home directory:

    mkdir ~/bin

    or

    mkdir /home/user2/bin

    or

    mkdir $HOME/bin
  13. Type the command to add the ~/bin folder to your PATH so that it is the first directory bash searches for binaries:

    PATH="$HOME/bin:$PATH"

    PATH=~/bin:$PATH or PATH=/home/user2/bin:$PATH are equally valid.

  14. To guarantee the value of PATH remains unaltered across reboots, what piece of code — in the form of an if statement — would you put into ~/.profile?

    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi

Answers to Explorational Exercises

  1. let: more than arithmetic expression evaluation:

    • Do a manpage or web search for let and its implications when setting variables and create a new local variable named my_val whose value is 10 — as a result of adding 5 and 5:

      let "my_val = 5 + 5"

      or

      let 'my_val = 5 + 5'
    • Now create another variable named your_val whose value is 5 — as a result of dividing the value of my_val into 2:

      let "your_val = $my_val / 2"

      or

      let 'your_val = $my_val / 2'
  2. The result of a command in a variable? Of course, that is possible; it is called command substitution. Investigate it and study the following function named music_info:

    music_info(){
    latest_music=`ls -l1t ~/Music | head -n 6`
    echo -e "Your latest 5 music files:\n$latest_music"
    }

    The result of the command ls -l1t ~/Music | head -n 6 becomes the value of the variable latest_music. Then the variable latest_music is referenced in the echo command (which outputs the total number of bytes occupied by the Music folder and the latest five music files stored in the Music folder — one per line).

    Which of the following is a valid synonym for

    latest_music=`ls -l1t ~/Music | head -n 6`

    It is option A:

    latest_music=$(ls -l1t ~/Music| head -n 6)

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Next Lesson

105.1 Customize and use the shell environment (105.1 Lesson 3)

Read next lesson

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

LPI is a non-profit organization.

Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. With more than 200,000 certification holders, it's the world’s first and largest vendor-neutral Linux and open source certification body. LPI has certified professionals in over 180 countries, delivers exams in multiple languages, and has hundreds of training partners.

Our purpose is to enable economic and creative opportunities for everybody by making open source knowledge and skills certification universally accessible.

  • LinkedIn
  • flogo-RGB-HEX-Blk-58 Facebook
  • Twitter
  • Contact Us
  • Privacy and Cookie Policy

Spot a mistake or want to help improve this page? Please let us know.

© Copyright 1999-2020 The Linux Professional Institute Inc. All rights reserved.