environment variables Flashcards

1
Q

What command displays the currently set CLI environment variables?

A

env

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

What does the term “session” mean?

A

refers to the state of being logged in to a computer’s command line interface.

When you log in, you start a new session, in which your commands will be recorded and other contextual information will be maintained.

When you close Terminal or type “exit”, your session is closed and that context and data is lost.

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

How do you set an environment variable on the fly to only affect the current session?

A

Set the variable on its own line, then use it anywhere:
$ SOMETHING=’some value’
$ echo $SOMETHING
some value

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

Name some environment files

A

.bashrc, .bash_profile, .bash_login, .login, .profile

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

What happens when you open a Terminal or connect to a server?

A
  1. You are logged into the command line with a user account. and put in the home dir for that user.
  2. One or more environment files are executed. These files set environment variables.
    E.g. .bashrc, .bash_profile, .bash_login, .login, .profile
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what do environment files do?

A

Store environment variables and allow you to modify or create environment variables.

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

How do you set an environment variable to temporarily change a variable before it gets used in a command?

A

Set the variable immediately preceding the command on the same line:

$ SOMETHING='a value' env
...
SOMETHING=a value
...
NOTE: that var is set ONLY for the executed command
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the correct syntax to set a variable?

A

that there should be no spaces between the variable and the equal sign or the equal sign and the value.

E.g.
SOMETHING=’some value’

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

When setting a value for a variable, are quotations required?

A

No.

But you must do it when the value has special characters.

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

what does the export command in an environment files like .bashrc or .bash_profile mean?

A

It means “make this variable available globally,” thus the variable is available and can be invoked from anywhere in CLI

A variable assignment that starts with export in your .bashrc will be available in the CLI session when you log in.

Note that if an environment variable has been exported once, you don’t need to continually put export before it when you set its value.

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

Can you use variables as a command?

A

Yes.

E.g.
$ MESSAGE='Hello, world!'
$ COMMAND="echo"
$ $COMMAND $MESSAGE
Hello, world!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When Interpolating Strings (aka displaying variable values in other strings), what must you do to the string that is invoking the variables?

A

The string must be surrounded in double quotation marks (“), not the single quotes (‘).

E.g.
$ MESSAGE1=”This is message 1.”
$ MESSAGE2=”This is message 2.”
$ MESSAGE=”$MESSAGE1 $MESSAGE2”

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

What variable determines which directories are searched when a command is entered?

A

PATH

It tells the CLI session where to find executable files for common commands, such as echo or pwd.

Note: When you type a word into the command line, and it doesn’t start with a /, ~, or a . (because those would indicate a path to an actual file), the command line will search each of the directories listed in the PATH environment variable for that command.

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

what does bin in /usr/local/bin stand for?

and what is the bin directory?

A

binary

a standard directory name that contains executable files, or programs.

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

what command reveals which executable file is executed when a command is run?

A

which
[which][command]

E.g. which pwd
/bin/pwd

It searches the paths for the named command and displays the first one it finds

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

How do you make a custom executable file executable like a built-in command?

A
  1. make sure it has executable permissions
  2. add the path to the directory it is contained in, to the PATH variable in ~/.bashrc:

E.g.
PATH=”/path/to/my/executables-directory:$PATH”

17
Q

When adding a path to a directory, like so:
PATH=”/path/to/my/executables-directory:$PATH”

why is $PATH needed at the end?

A

This preserves the old PATH locations while making your directory the highest priority.

18
Q

What is the PATH environment variable?

A

an ordered, colon-delimited, list of directories that contain executables

19
Q

Does the order of the paths set in PATH matter?

A

Yes.

The order of the directories in the PATH variable is first-found-first-execute

20
Q

What happens if you use a /, ., or ~ before a command?

A

The command line will interpret that as an actual path to a file, and will not use the PATH variable

21
Q

where should permanent modifications to environment variables be stored?

A

in environment files such as .bashrc, .bash_profile, .bash_login, .login, .profile

22
Q

what is this command?&raquo_space;

A

redirection operator

23
Q

what operator appends text to a file?

A

> >

24
Q

what will this do?

echo ‘blah’&raquo_space; ~/some_unique_file_name.txt

A

creates the file and appends ‘blah’ to its contents

25
Q

what will this do?

echo ‘blah’&raquo_space; ~/pre_existing_file.txt

A

appends ‘blah’ to the contents of the file

26
Q

____ provides context for the commands you run.

A

environment variables

27
Q

How do you permanently set environment variables?

A

exporting the variables in an environment file like ~/.bashrc