Command Line Variables

Hands-On Lab

 

Photo of Michael Christian

Michael Christian

Course Development Director in Content

Length

00:30:00

Difficulty

Beginner

In this hands-on lab, we're going to practice setting and using variables in Bash. Variables are an important part of the commadn line, as well as writing shell scripts. Becoming familair and comfortable with environment variables is an important step in using Linux.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Command Line Variables

Introduction

In this hands-on lab, we're going to practice setting and using variables in Bash.

Connecting to the Lab

  1. Open your terminal application, and run the following command (remember to replace <PUBLIC_IP> with the public IP you were provided on the lab instructions page):
    ssh cloud_user@<PUBLIC_IP>
  2. Enter yes at the prompt.
  3. Enter your cloud_user password at the prompt.

Examine the Current $PATH Variable

  1. Determine the current working directory.
    pwd
  2. List the contents of the current directory.
    ls
  3. List the contents of the scripts folder.
    ls scripts/
  4. Run the test.sh script, specifying the path to it.
    ./scripts/test.sh
  5. Attempt to run the test.sh script without specifying the path to it.
    test.sh
  6. List the current environment variables, and locate the $PATH variable.
    env
  7. Examine the $PATH variable.
    echo $PATH

Append the Path to the scripts Directory to the $PATH Variable

  1. Run the following command:
    PATH="$PATH:$HOME/scripts"
  2. Run the test.sh script again without specifying the path.
    test.sh

Make the New Path Persist

  1. View the contents of the .profile file.
    cat .profile
  2. View the $PATH variable.
    echo $PATH
  3. Source the .profile file.
    source .profile
  4. Modify the ~/.profile file to make our change permanent.
    echo 'PATH="$PATH:$HOME/scripts"' >> ~/.profile
  5. Source the .profile file again.
    . .profile
  6. View the $PATH variable again.
    echo $PATH
  7. Run the test.sh script without specifying the path again.
    test.sh

Conclusion

Congratulations, you've successfully completed this hands-on lab!