2.1 Command Line Basics - Part 2 Flashcards

1
Q

What is a variable in bash?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two types of variables?

A
  1. Local - which are available to the current shell processes only.
  2. 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False: Variables persist through the closure of a shell session.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you set a local variable in bash?

A

By using the “=” operator

ex:

greeting=hello
echo greeting
hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which command removes a variable in bash?

A

unset

ex:

greeting=hello
echo $greeting
hello
unset greeting
echo $greeting
[nothing gets printed]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which command creates an ‘environmental variable’?

A

export

ex:

export greeting=hello

(NOTE: You can use export as a command at the same time you assign the greeting variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the “PATH” variable in bash and why it’s important

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What command would you use to append a new directory to the PATH variable?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly