2.1 Command Line Basics - Part 2 Flashcards
What is a variable in bash?
Variables are pieces of storage for data.
Once set, a variable’s value can be accessed at a later time by calling the variable’s name
ex:
$PATH
What are the two types of variables?
- Local - which are available to the current shell processes only.
- Environmental - which are available in both a specific shell session and in sub processes spawned from that shell session.
Programs can access environmental variables only. Environmental variables are in capital letters (e.g. PATH. USER, DATE)
True or False: Variables persist through the closure of a shell session.
False - All variables and their contents are lost.
Most shells provide configuration files that contain variables which are set whenever a new shell is started.
How do you set a local variable in bash?
By using the “=” operator
ex:
greeting=hello
echo greeting
hello
Which command removes a variable in bash?
unset
ex:
greeting=hello
echo $greeting
hello
unset greeting
echo $greeting
[nothing gets printed]
Which command creates an ‘environmental variable’?
export
ex:
export greeting=hello
(NOTE: You can use export as a command at the same time you assign the greeting variable)
Explain the “PATH” variable in bash and why it’s important
The PATH variable is an environmental variable that stores a list of directories, separated by a colon, that contains executable programs eligible as commands from the Linux shell.
Any programs you want to execute within the shell must be contained within the PATH variable
What command would you use to append a new directory to the PATH variable?
PATH=$PATH:new_directory
The “=” operator assigns the value to the PATH variable. The “:” operator is needed to append (instead of replacing) the existing PATH variable.
ex:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:
PATH=$PATH:/home/user/bin
echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/home/user/bin