Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 1h 0m
Published
Clock icon Nov 27, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Discover Available Shells, Use Privilege Elevation Commands, and View and Manipulate User History

    See what shells are available in the environment

    cat /etc/shells
    

    Determine what shell we're using:

    set | grep SHELL
    

    Find our user ID, group ID, and groups we're secondary members of:

    id
    

    Elevate privileges and run id:

    sudo -i id
    

    Find out who you're logged in as:

    whoami
    

    Elevate privileges and run whoami:

    sudo -i whoami
    

    View the history stack:

    history
    

    Write the history stack to a file:

    history > shell-history.txt
    

    Find the times you used whoami:

    grep -i whoami shell-history.txt
    

    Alternatively

    whoami <up arrow>
    

    run the last command

    !!
    

    run a history command by number

    !<number> ex !48
    
  2. Challenge

    Explore System Variables, Create and Use Custom Variables, and Understand Special Variables

    Create a script:

    vim uservar.sh
    

    Contents

    #/bin/bash
    
    echo Hello, my name is $USER
    echo My $PRODUCT costs 10 dollars
    

    run it

    bash uservar.sh
    

    Create the $PRODUCT variable and rerun the script

    PRODUCT=Snowblower
    
    bash uservar.sh
    

    Find out which shell level we're on:

    echo $SHLVL
    

    Edit the script: At the top of the file, right below #/bin/bash, enter:

    echo Shell is at level $SHLVL
    

    Run the script again:

    bash uservar.sh
    

    We need to export the variable to make it available to the current shell:

    export PRODUCT
    

    Run the script again:

    bash uservar.sh
    

    Edit the script:

    vim uservar.sh
    

    Replace 10 dollars with $COST dollars USD. Save and quit the file. Declare a COST variable:

    export COST=100
    

    View the file's contents:

    cat uservar.sh
    

    Run the script again:

    bash uservar.sh
    

    Edit the script:

    vim uservar.sh
    

    Change the PRODUCT variable 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.sh
    

    Run the script:

    bash uservar.sh
    

    Edit the script:

    vim uservar.sh
    

    At 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, $1 is the color of the snowblower and $2 is the amount in stock. Save and quit. View the file's contents:

    cat uservar.sh
    

    Run the script:

    bash uservar.sh
    

    Enter the arguments:

    bash uservar.sh Blue 10
    
  3. 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 -1
    

    Send the output to lsoutput.txt:

    ls -1 > lsoutput.txt
    

    Get its word count:

    wc -l lsoutput.txt
    

    View the file's contents, using it as an argument:

    cat lsoutput.txt
    

    View the file's contents, using it as an input:

    cat < lsoutput.txt
    

    The < is an input redirect.

    Pipe the output to the nl (numbered lines) command:

    cat lsoutput.txt | nl
    

    This has each item on its own numbered line.

    Pipe the output to the nl and tac (i.e., the opposite of cat) commands:

    cat lsoutput.txt | nl | tac
    

    This 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 Denied errors).

    Put the good (or stdout) data in procdata.txt:

    find /proc -iname "*.*" > procdata.txt
    

    We'll still see all the error messages.

    View the procdata.txt file's contents:

    cat procdata.txt
    

    We should see good/stdout data.

    Send the stderr data to badprocdata.txt:

    find /proc -iname "*.*" 2> badprocdata.txt
    

    Put the stdout data in procdata.txt and get rid of the stderr data:

    find /proc -iname "*.*" > procdata.txt 2> /dev/null
    

    View the file's contents:

    cat procdata.txt
    

    Use the tee command to write the stdout data to a file and send the rest to the console:

    find /proc -iname "*.*" | tee procdata.txt
    

    Discard the stderr data and send the stdout data to the file:

    find /proc -iname "*.*" 2> /dev/null | tee procdata.txt
    

    Using a ; between commands makes them both run, no matter if one throws an error:

    ls ; farg
    

    Here, farg is 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 || farg
    

    Because ls worked, farg did not run.

    Switch them around:

    farg || ls
    

    Since farg failed, it had to failover to ls. Using double ampersands means "run the next command only if the first command is successful." Try it:

    ls && farg
    

    Since ls worked, it also ran farg. Reverse the order:

    farg && ls
    

    Because farg failed, ls did not run.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans