Shell Variables Flashcards

1
Q

Shell variables are assigned using an..?

A

A=apple syntax

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

Variables are examined (“dereferenced”) with the …?

A

$ character, as in echo $A

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

At the kernel level, every process has a collection of environment variables, which are inherited by …?

A

child processes.

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

The export command converts a shell variable into an…?

A

environment variable.

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

The set and env commands list…?

A

shell variables and environment variables, respectively.

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

The bash shell allows users to …?

A

set and reference shell variables.

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

A shell variable is simply a …?

A

named value that the shell remembers.

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

Shell variables can be used in…?

A

commands and shell scripts and can also be referenced by programs as configuration options.

(For example, the mutt email client runs an external editor when composing a message. By default this editor is vi. However, before running vi it will check to see if a variable called EDITOR has been set. If it has, then the command defined by EDITOR is used instead of vi. Most programs that launch external editors work the same way.)

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

There are two types of shell variables…?

A

local variables and environment variables.

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

A local variable exists only within…?

A

the shell which it is created.

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

Environment variables are inherited by…?

A

child shells such as when a graphical terminal is launched after logging in.

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

How to set a local variable…?

A

A = apple.

(don’t place any spaces on either side of the = sign. Now the shell will “hang on” to this association for as long as the shell exists (or until it is explicitly unset,)

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

Whenever prince would like to use the value “apple”, he can use use the variable A instead, preceding the variable with a…?

A

dollar sign ($), as in echo $A

This is called dereferencing the variable A.

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

The variable can be used anywhere on the command line (or in shell scripts). What if prince, waxing poetic, decided to write a few lines about apples, which he wanted to save in a file called ode_to_apple.txt. The following line could get him started…..?

A

[prince@station prince]$ echo “Oh, I like them squishy”&raquo_space; ode_to_$A.txt
[prince@station prince]$ ls
ode_to_apple.txt

When the bash shell examined the command line, it replaced $A with apple. These are the basics of shell variables. Variables are established, and set, with a VAR=value syntax, and dereferenced with a $VAR syntax.

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

What can be used for variable names?

A

Variable names can be any string of alphanumeric characters (A-Z, a-z, 0-9), and the underscore (_), but cannot start with a number

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

Shell variables are case…?

A

sensitive.

[prince@station prince]$ B=banana
[prince@station prince]$ echo $B is my favorite fruit
banana is my favorite fruit
[prince@station prince]$ echo $b is my favorite fruit
is my favorite fruit

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

What can be a variable’s value?

A

Anything

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

When assigning variables, the syntax is name=value, with no spaces. What if prince wanted the variable FRUIT to resolve to the phrase mushy bananas?

A

[prince@station prince]$ FRUIT=mushy bananas
-bash: bananas: command not found

We have stumbled into an advanced syntax for setting variables, namely name=value command, which sets the variable name only for the execution of the specified command. The bash shell dutifully set the variable FRUIT to the value mushy, and went to execute the command bananas, with expectable results. All of this is not the important bit. The important bit is that if you want to set a variable to a value which contains spaces, you must include the value in quotes.

[prince@station prince]$ FRUIT=”mushy bananas”
[prince@station prince]$ echo $FRUIT is my favorite fruit
mushy bananas is my favorite fruit

With this modification, prince gets the correct behavior from the bash shell, if not correct English grammar.

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

When dereferencing variables, the variable name can be marked using…?

A

braces {}, if need be.

see https://academy.redhat.com/courses/rha030-6.1/rha030_bash_shvar.html for example.

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

When finished with a variable, the variable may be unbound from its value with the…?

A

unset command.

[prince@station prince]$ unset A
[prince@station prince]$ echo $A

[prince@station prince]$

21
Q

Bash Read Only Variables:
Variable: ?
Expands to: ??????

A

The exit status of the most recently executed command.

22
Q

Bash Read Only Variables:
Variable: -
Expands to: ?????

A

Currently enabled shell option flags

23
Q

Bash Read Only Variables:
Variable: $
Expands to: ?????

A

Process id (pid) of current shell.

24
Q

Bash Read Only Variables:
Variables: !
Expands to:?????

A

Process id (pid) of most recent background command.

25
Q

Bash Read Only Variables:
Variables: _
Expands to: ?????

A

Last token of previous command.

26
Q

Bash Read Only Variables:
Variables: PPID
Expands to: ????

A

The process id (pid) of the shell’s parent.

27
Q

Bash Read Only Variables:
Variables: SHELLOPTS
Expands to: ?????

A

Colon separated list of currently enabled shell options, as reported by the set -o command.

28
Q

Bash Read Only Variables:
Variables: UID
Expands to: ???

A

The userid of the current user.

29
Q

Bash Preassigned Variables:
Variable: BASH_VERSION
Expands to: ??????

A

The current bash version

30
Q

Bash Preassigned Variables:
Variable: HOSTNAME
Expands to: ?????

A

The DNS hostname of the current machine.

31
Q

Bash Preassigned Variables:
Variable: OLDPWD
Expands to:????

A

The previous working directory.

32
Q

Bash Preassigned Variables:
Variable: PWD
Expands to:????

A

The current working directory.

33
Q

Bash Preassigned Variables:
Variable: RANDOM
Expands to:????

A

A random number between 0 and 32767

34
Q

Bash Preassigned Variables:
Variable: SECONDS
Expands to:????

A

The number of seconds since the shell was started.

35
Q

Just as the bash shell allows users to assign name-value pairs called shell variables, the Linux kernel allows any process to….?

A

define name-value pairs called environment variables.

These variables are a part of the process stored in the kernel, just as the process id, user id, and current working directory are part of the process. More importantly, whenever a process starts another process (such as the bash shell starting the ls command).

36
Q

Environment variables are…?

A

nherited by the child process. This allows users to use the bash shell to create or modify an environment variable, and then all commands started by the shell will inherit that variable.

37
Q

How do we create environment variables within the bash shell…?

A

First, a shell variable is created, and then the shell variable is “promoted” to an environment variable using the export command. (The variable will then be exported to any future child processes).

Examples:
https://academy.redhat.com/courses/rha030-6.1/rha030_bash_shvar.html

38
Q

Environment variables are often used to…?

A

configure commands with information about local configurations, or in other words, information about the local environment. As an example, many commands will look for an environment variable called LANG to determine the user’s language, and modify their output accordingly.

See : https://academy.redhat.com/courses/rha030-6.1/rha030_bash_shvar.html

39
Q

By setting the LANG environment variable to…?

A

de_DE, the abbreviation for the day “Friday” becomes the customary German abbreviation.

40
Q

By setting LANG to es_ES, the effects are…?

A

even more obvious, as the day’s and the month’s abbreviations have changed to Spanish (as well as capitalization conventions).

An important point deserves restating. The date command did not change behavior because the bash command had an environment variable called LANG (directly). The process running the date command modified its output because it had its own environment variable called LANG. It just happened to inherit this variable from the bash shell. All processes have environment variables, not just shells.

Why didn’t prince have to explicitly export the variable LANG? The variable is already an environment variable, set by startup scripts. Once a variable is an environment variable, it can be modified (and removed) using the same syntax as shell variables.
Often, users use a shorter syntax to both create and export an environment variable:
[prince@station prince]$ export EDITOR=nano
With this single command, prince has created, assigned, and exported the variable EDITOR.

41
Q

The bash shell provides two commands for listing which variables are defined….?

A

set and env.

42
Q

The set command, without arguments, lists…?

A

bothshell variables and environment variables associated with the shell

43
Q

the env command, again without arguments, lists only…?

A

variables which have been exported to the environment.

44
Q

Commonly Used Environment Variables:
Variable: TERM
Use: ??????

A

Specifies low level configuration of the user’s terminal. This variable is more relevant when using a serial line console (“dumb terminal”) to access the system.

45
Q

Commonly Used Environment Variables:
Variable: PATH
Use: ??????

A

Specifies directories to be searched for executable files.

46
Q

Commonly Used Environment Variables:
Variable: DISPLAY
Use: ??????

A

Specifies which X server clients should use in the graphical environment.

47
Q

Commonly Used Environment Variables:
Variable: LANG
Use: ??????

A

Specifies the preferred language for internationalized programs.

48
Q

Commonly Used Environment Variables:
Variable: EDITOR
Use: ??????

A

Many programs rely on an external editor for user input. Often, the default editor is vi. If the EDITOR environment variable is set, the specified editor will be used instead.

49
Q

Commonly Used Environment Variables:
Variable: PRINTER
Use: ??????

A

Most commands that submit or manage print jobs will examine this environment variable to determine the default printer.