Skip to content

Latest commit

 

History

History
167 lines (156 loc) · 2.62 KB

File metadata and controls

167 lines (156 loc) · 2.62 KB

Hands-on Linux-02 : Linux Environment Variables

Purpose of the this hands-on training is to teach the students how to use environment variables.

Learning Outcomes

At the end of the this hands-on training, students will be able to;

  • explain environment variables.

  • understand Quoting with Variables.

Outline

  • Part 1 - Common Environment Variables & Accessing Variable

  • Part 2 - Path Variable

  • Part 3 - Quoting with Variables

  • Part 4 - Sudo Command

Part 1 - Common Environment/Shell Variables & Accessing Variable

  • Difference between "env" and "printenv" commands. ​
env
printenv
printenv HOME
echo $HOME
env HOME

  • Understanding the shell variable. ​
CLARUS=way
env
set
set | grep CLARUS
echo $CLARUS

  • Understanding the environment variable. Use export command. ​
export WAY=clarus
env

  • Difference between shell and environment variables. Create a user, name it "user1", switch to user1, check the environment and shell variables. ​
export WAY=clarus
sudo su
useradd user1
passwd user1 # give user1 any password.
exit
su user1
env | grep WAY
set | grep CLARUS

  • Change the environment variable value. ​
export WAY=linux
env
export WAY=script
env

  • Remove the environment variable with unset command. ​
export WAY=clarusway
env | grep WAY
unset WAY
env | grep WAY

Part 2 - Path Variable

  • PATH variable. ​
printenv PATH
cd /bin
ls ca*    # see the cat command.

  • Add a path to PATH variable for running a script. ​
cd
mkdir test && cd test
nano test.sh
# copy and paste the code-echo "hello world"- in test.sh
chmod +x test.sh
./test.sh
cd    # change directory to ec2-user's home directory
./test.sh    # it doesnt work. 
./test/test.sh
printenv PATH
cd test
pwd
export PATH=$PATH:/home/ec2-user/test
printenv PATH
cd
test.sh
cd /
test.sh

  • Using the environment variable in the script. ​
cd test
export CLARUS=env.var
WAY=shell.var
cd test
nano test1.sh
# copy and paste the code-echo "normally we should see env. variable $CLARUS but probably we can't see the shell variable $WAY "
chmod +x test1.sh
./test1.sh

Part 3 - Quoting with Variables.

  • Double Quotes. ​
MYVAR=my value
echo $MYVAR
MYVAR="my value"
echo $MYVAR
MYNAME=james
MYVAR="my name is $MYNAME"
echo $MYVAR
MYNAME="james"
MYVAR="hello $MYNAME"
echo $MYVAR
MYVAR="hello \$MYNAME"
echo $MYVAR

  • Single Quotes. ​
echo '$SHELL'
echo 'My\$SHELL'

Part 4 - Sudo Command.

  • Sudo Command. ​
yum update
sudo yum update
cd /
mkdir testfile
sudo su
sudo -s
sudo su -