Like every programming language, Bash has variables.
A variable is a name that holds a value.To read its value, you put
$before its name like this:netops@automation:~/automation$ echo $HOME /home/netops
Figure 1 - $ Reads the Value of a Variable
Your shell already has a few variables, like
HOMEandSHELL.In the lab, print the value of the variable
SHELL.Answer the question below
What is the value of SHELL in the lab?
To create a variable, you write its name, the sign
=and its value like this:netops@automation:~/automation$ DEVICE_IP=10.0.0.1 netops@automation:~/automation$ echo $DEVICE_IP 10.0.0.1
Figure 2 - A Name, the = Sign and a Value
In the lab, create
DEVICE_IPwith the value10.0.0.2and print it to get the flag.Answer the question below
Enter the flag you obtained.
A script creates and reads variables like your shell:
BACKUP_DIR=/home/netops/backups echo "Saving to $BACKUP_DIR"In the lab, change
BACKUP_DIRto/home/netops/archiveinstart_backup.sh, make the script executable and run it to get the flag.Answer the question below
Enter the flag you obtained.
A script does not see the variables of your shell.
Take the scriptbackup.sh:#!/bin/bash echo "Backing up $DEVICE_IP"You create
DEVICE_IPin your shell, but the script prints no address:netops@automation:~/automation$ DEVICE_IP=10.0.0.1 netops@automation:~/automation$ ./backup.sh Backing upTo pass a variable to your scripts, you use the command
exportlike this:netops@automation:~/automation$ export DEVICE_IP netops@automation:~/automation$ ./backup.sh Backing up 10.0.0.1
Figure 3 - export Passes the Variable to the Script
An exported variable is called an environment variable.
In the lab, you need to:
Make
backup.shexecutable.Make it print
Backing up 10.0.0.2.
Answer the question below
Enter the flag you obtained.
To list all your environment variables, you use the command
envlike this:netops@automation:~/automation$ env ... USER=netops ... SHELL=/bin/bash LANG=C.UTF-8 ...Each line gives the name of a variable, the sign
=and its value.In the lab, list your environment variables and find the value of
LANG.Answer the question below
What is the value of LANG in the lab?
To delete a variable, you use the command
unsetlike this:netops@automation:~/automation$ DEVICE_IP=10.0.0.1 netops@automation:~/automation$ echo $DEVICE_IP 10.0.0.1 netops@automation:~/automation$ unset DEVICE_IP netops@automation:~/automation$ echo $DEVICE_IPechoprints an empty line: the variable is gone.Answer the question below
Which command deletes a variable?