Linux Exercise: Customize and use the shell environment

Exporting shell variables

  1. Make a list of all shell variables in use in your shell
    • # set
  1. Make a list of all shell variables in use, that are exported
    • # env
  1. Print the PID of the current shell
    • # echo $$
  1. Create a shell variable "var1" with the value "bla". Check whether the variable was set correctly.
    • # var1=bla
    • # echo "$var1"
  1. Start a subshell underneath your current shell. Verify you are indeed in a subshell by looking at the PID and the process hierarchy.
    • # bash
    • # echo $$
      You should now see a different PID.
    • # pstree -p
      You should now see the process hierarchy, with your current shell as a subprocess of your parent shell.
      Note: pstree might not be installed. It is part of the psmisc RPM so you can install it with yum -y install psmisc.
  1. Look at the value of the variable "var1". What is the value? Is this correct?
    • # echo "$var1"
      The variable is empty.
  1. End your subshell. Verify you are again in your parent shell. Now export the variable "var1" and start a subshell again. Display the contents of the variable "var1".
    • # exit
    • # echo $$
    • # export var1
    • # env | grep ^var1
    • # bash
    • # echo $$
    • # echo "$var1"
  1. While still in your subshell, change the contents of the "var1" variable. Verify the variable was set properly. Now end the subshell and in your parent shell, list the contents of the "var1" variable again. What happens?
    • # var1="bla bla"
    • # echo "$var1"
    • # exit
    • # echo $$
    • # echo "$var1"
      The variable still has the old value: Variables are copied from a parent process to a child process when a child process is started, but are not copied back to the parents' environment when the child process exits.
  1. Delete the variable "var1" and verify that it was indeed deleted.
    • # unset var1
    • # set | grep ^var1
    • # echo "$var1"

Using built-in shell variables

  1. Locate the executable of the "ls" command.
    • # which ls
  1. Verify this directory is in your search path.
    • # echo "$PATH"
      Look for the /bin or /usr/bin directory.
  1. Save your path variable in a "SAVEPATH" variable. Change your search path so that the previously found directory is no longer in your path. Try to execute the "ls" command. Does this work?
    • # SAVEPATH="$PATH"
    • # PATH=/usr/local/bin
    • # ls
      This command should give you an error: Command not found. This is because the shell only looks for directories in $PATH when trying to search for an executable.
  1. List the contents of the $PATH variable using the "echo" command. Does this work?
    • # echo "$PATH"
      This command will work regardless of the contents of the $PATH variable. "echo" is a shell builtin.
  1. Force the execution of the "ls" command despite the current setting of the $PATH variable.
    • # /bin/ls
      This will work regardless of the contents of the $PATH variable. When you provide an absolute or a relative path to an executable (which you do because there are forward shashes in the string), the shell will use that particular executable, and will not use the search path at all.
  1. Return your $PATH to its previous state.
    • # export PATH="$SAVEPATH"
  1. Save the value of the PS1 variable in a SAVEPS1 variable. Change the PS1 variable into "Type a command: ". What happens? Restore the PS1 variable.
    • # SAVEPS1="$PS1"
    • # PS1="Type a command: "
      Your prompt will have changed into "Type a command: "
    • Type a command: PS1="$SAVEPS1"
      Your prompt is now back to its former self.

Using the set command

  1. Use output redirection to write the text "bla" in a file "bla".
    • # echo "bla" > bla
  1. Now execute the command again, but with a different text. What is the contents of "bla"?
    • # echo "bla bla" > bla
    • # cat bla
  1. Enable the shell option "noclobber". Again, try to write something to bla, or add something to bla. What happens?
    • # set -o noclobber
    • # echo "bla bla bla" > bla
    • # cat bla
    • # echo "bla bla bla bla" >> bla
    • # cat bla
  1. Make a list of all shell options.
    • # set -o

User environment

  1. Look at the contents of the following files: /etc/profile, /etc/profile.d/* ~/.bash_profile, ~/.bashrc, ~/.bash_logout
    • # cat /etc/profile
    • # ls /etc/profile.d/*
    • # cat /etc/profile.d/...
    • # cat ~/.bash_profile
    • # cat ~/.bashrc
    • # cat ~/.bash_logout
  1. Change the files you displayed in the previous step, so that on one of the first lines there is a command "echo this is ...". Logout and log in again. Does the result match your exectations
    • # vi /etc/profile
      Add the line:
      echo "This is /etc/profile"
    • Do the same for the other files.
  1. Also, perform an "su" and an "su -" to the root account. Is the result what you expected?
    • # su
    • # exit
    • # su -
    • # exit
  1. Change your .bash_profile so that it contains an exported variable "var1". Log out and log in again. Is the variable set?
    • # vi ~/.bash_profile
      Add the line:
      export var1="bla"
    • Logout, then log in again.
    • # echo "$var1"

Aliases

  1. Make a list of all aliases in your current shell
    • # alias
  1. Set an alias "ll", which executes the "ls -l" command. Try the "ll" command. Does it work?
    • # alias ll='ls -l'
    • # ll
  1. Remove the "ll" alias. Verify the command no longer works.
    • # unalias ll
    • # ll
  1. Add the alias for ll to your .bash_profile. Logout and login again, and verify the "ll" command now works.
    • # vi ~/.bash_profile
      Add the line:
      alias ll='ls -l'
    • Logout, login#
    • # ll

Functions

  1. In your .bash_profile, define a function "usercount" that prints how many unique users are currently active on the system. Logout, then log in again and see if you can use the function.
    • # vi ~/.bash_profile
      Add the following:
      function usercount {
          w | tail -n +2 | awk '{print $1}' | sort -u | wc -l
      }
    • Logout, login
    • # usercount
End of exercise