Intermediate Bash - Environment and Variables

Question Click to View Answer

Display the shell variables, environment variables, and shell functions that are available in your shell.

set

The output from set can be long and is more readable if it's piped to a pager program like less:

set | less

Print all the environment variables that are available in your shell.

printenv

Get the value associated with the HOME environment variable.

# one method
printenv HOME

# another method
echo $HOME

Assign the variable RAPPER to the value 'ludacris'. What type of variable is RAPPER?

RAPPER='ludacris'

RAPPER is a shell variable (it is not an environment variable).

Print the process id (aka PID) of the shell.

echo $$

Print the parent process ID (aka PPID) of the shell. Explain what a parent process is.

echo $PPID

Print the shell PID, start a child process, and then verify that the PPID of the child equals the shell's PID.

# get shell PID
echo $$

# start child process
bash

# get PPID of child process
echo $PPID

The PPID of the child process is the same as the shell's PID.

Create a shell variable called MY_AGE, start a child process and demonstrate that the shell variable is not accessible in the child process.

MY_AGE="getting older..."

# start child process
bash

# MY_AGE is not accessible
echo $MY_AGE

Create an environment variable called CAR and assign it to the value "fly whip".

CAR="fly whip"
export CAR

Create an environment variable called GOLF and assign it to the value "ball". Create a child process and demonstrate that the GOLF environment variable is accessible in the child process.

GOLF='ball'
export GOLF

# create child process
bash

# GOLF variable is still accessible
echo $GOLF

Set the COUNTER variable to 8 and then increment COUNTER by one.

COUNTER=8
COUNTER=$(( $COUNTER + 1 ))

Create a variable called DIRS and assign it to the value "/usr/bin:/bin". Update the DIRS variable to also include the "/fake_dir/bin" directory.

DIRS="/usr/bin:/bin"
DIRS=$DIRS:/fake_dir/bin
echo $DIRS

This same technique can be used to add a directory to the PATH environment variable.