Lab 2
Lab 2
Page | 1
5. If you enter a variable or alias, it only lasts during this session. You can instead define these
in one of several files so that they are available in any and every shell.
a. First, let’s do an experiment. Type X=0 <enter>. Type echo $X. Now type bash.
This starts a subshell (or inner session). Type echo $X. What response do you get?
Why? Type exit to resume your outer session and type echo $X. What did you get
this time?
b. We can make a variable accessible in other shells by exporting it. Type export X.
Type bash and from the inner session, type echo $X. What response did you get now?
Type X=1 to change X’s value. Type exit and echo $X. What value is X in this outer
shell? Why? Type export to list all exported variables in this shell. All but one of them
are environment variables defined in various scripts; one is X. It should say X=“1”. Type
exit to return to your outer session and type export. You will find in this list X=“0”
instead.
c. Type cd ~ to return to your home directory if you are not already there. Type
cat .bashrc. This script runs every time you start a new Bash session. It performs
only three operations: if /etc/bashrc exists, it runs that script; it adds two directories to
your PATH variable and it exports PATH. If you explore /etc/bashrc, you will find that
that script creates other environment variables like PROMPT_COMMAND, PS1 and
PATH. If you want to alter the values of your PS1 or PATH variables, would you do so
from the command line, from /etc/bashrc or ~/.bashrc? Why?
d. In your home directory, look at the contents of the file ~/.bash_profile. What does this
script do that .bashrc did not?
e. In comparing the two files (.bashrc, .bash_profile), there is a comment at the bottom of
each that explain what you might use the file for. What should you use .bashrc for and
what should you use .bash_profile for?
f. While in your home directory, type echo “alias me=‘ps aux | grep
Student’” >> .bashrc. This instruction is an echo statement to output the text
“alias me=‘ps aux | grep Student’” and redirect the output to the file .bashrc. View
your .bashrc file and you should find this entry at the bottom of the file. Thus, you have
added an alias to the file. You would more conveniently edit .bashrc (or .bash_profile)
using vi, but you haven’t learned vi yet (it’s the topic of the next lab). Explain what the
alias does. Type me. Why did it not work? Even though we’ve changed the .bashrc file,
we need our Bash session to re-read this file. To do so, type source .bashrc. Type
me What happens this time? Type alias to see that me has been defined. What does
source do?
Page | 2