- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Using the Shell, History, Variables, and Redirection
The shell is the basic element of interaction with a Linux system. No matter what GUI desktops may exist, the shell is always there and ready to execute your commands. In this lab, we'll explore what shells are available, what makes up the shell environment, and the different ways to log in, executing the shell's environment as needed for our task.
Lab Info
Table of Contents
-
Challenge
Discover Available Shells, Use Privilege Elevation Commands, and View and Manipulate User History
See what shells are available in the environment
cat /etc/shellsDetermine what shell we're using:
set | grep SHELLFind our user ID, group ID, and groups we're secondary members of:
idElevate privileges and run
id:sudo -i idFind out who you're logged in as:
whoamiElevate privileges and run
whoami:sudo -i whoamiView the
historystack:historyWrite the
historystack to a file:history > shell-history.txtFind the times you used
whoami:grep -i whoami shell-history.txtAlternatively
whoami <up arrow>run the last command
!!run a history command by number
!<number> ex !48 -
Challenge
Explore System Variables, Create and Use Custom Variables, and Understand Special Variables
Create a script:
vim uservar.shContents
#/bin/bash echo Hello, my name is $USER echo My $PRODUCT costs 10 dollarsrun it
bash uservar.shCreate the $PRODUCT variable and rerun the script
PRODUCT=Snowblower bash uservar.shFind out which shell level we're on:
echo $SHLVLEdit the script: At the top of the file, right below
#/bin/bash, enter:echo Shell is at level $SHLVLRun the script again:
bash uservar.shWe need to export the variable to make it available to the current shell:
export PRODUCTRun the script again:
bash uservar.shEdit the script:
vim uservar.shReplace
10 dollarswith$COST dollars USD. Save and quit the file. Declare aCOSTvariable:export COST=100View the file's contents:
cat uservar.shRun the script again:
bash uservar.shEdit the script:
vim uservar.shChange the
PRODUCTvariable line to:echo "My $PRODUCT costs \$$COST dollars USD"We have to include a
\to make the$properly show up. Save and quit. View the file's contents:cat uservar.shRun the script:
bash uservar.shEdit the script:
vim uservar.shAt the bottom of the file, enter:
echo My $PRODUCT comes in $1 and I have $2 of them in stock echo The executable name is $0 echo The arguments entered were $* echo The number of arguments were $# echo The PID of this shell is $$Here,
$1is the color of the snowblower and$2is the amount in stock. Save and quit. View the file's contents:cat uservar.shRun the script:
bash uservar.shEnter the arguments:
bash uservar.sh Blue 10 -
Challenge
Use Input/Output/Error Redirection to Execute Multiple Commands and Send Data to Disk
List the files output so they're each on a separate line:
ls -1Send the output to
lsoutput.txt:ls -1 > lsoutput.txtGet its word count:
wc -l lsoutput.txtView the file's contents, using it as an argument:
cat lsoutput.txtView the file's contents, using it as an input:
cat < lsoutput.txtThe
<is an input redirect.Pipe the output to the
nl(numbered lines) command:cat lsoutput.txt | nlThis has each item on its own numbered line.
Pipe the output to the
nlandtac(i.e., the opposite ofcat) commands:cat lsoutput.txt | nl | tacThis time, each item is numbered, but the list starts from the last number.
Run the following:
find /proc -iname "*.*"We'll see a lot of stdrr (
Permission Deniederrors).Put the good (or stdout) data in
procdata.txt:find /proc -iname "*.*" > procdata.txtWe'll still see all the error messages.
View the
procdata.txtfile's contents:cat procdata.txtWe should see good/stdout data.
Send the stderr data to
badprocdata.txt:find /proc -iname "*.*" 2> badprocdata.txtPut the stdout data in
procdata.txtand get rid of the stderr data:find /proc -iname "*.*" > procdata.txt 2> /dev/nullView the file's contents:
cat procdata.txtUse the
teecommand to write the stdout data to a file and send the rest to the console:find /proc -iname "*.*" | tee procdata.txtDiscard the stderr data and send the stdout data to the file:
find /proc -iname "*.*" 2> /dev/null | tee procdata.txtUsing a
;between commands makes them both run, no matter if one throws an error:ls ; fargHere,
fargis not an actual command, but both commands still run.Using double pipes means "if the first command fails, only then run the next command." Try it:
ls || fargBecause
lsworked,fargdid not run.Switch them around:
farg || lsSince
fargfailed, it had to failover tols. Using double ampersands means "run the next command only if the first command is successful." Try it:ls && fargSince
lsworked, it also ranfarg. Reverse the order:farg && lsBecause
fargfailed,lsdid not run.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.