Shell Variables Flashcards Preview

Networking > Shell Variables > Flashcards

Flashcards in Shell Variables Deck (49)
Loading flashcards...
1

Shell variables are assigned using an..?

A=apple syntax

2

Variables are examined ("dereferenced") with the ...?

$ character, as in echo $A

3

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

child processes.

4

The export command converts a shell variable into an...?

environment variable.

5

The set and env commands list...?

shell variables and environment variables, respectively.

6

The bash shell allows users to ...?

set and reference shell variables.

7

A shell variable is simply a ...?

named value that the shell remembers.

8

Shell variables can be used in...?

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.)

9

There are two types of shell variables...?

local variables and environment variables.

10

A local variable exists only within...?

the shell which it is created.

11

Environment variables are inherited by...?

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

12

How to set a local variable...?

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,)

13

Whenever prince would like to use the value "apple", he can use use the variable A instead, preceding the variable with a...?

dollar sign ($), as in echo $A

This is called dereferencing the variable A.

14

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.....?

[prince@station prince]$ echo "Oh, I like them squishy" >> 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.

15

What can be used for variable names?

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

16

Shell variables are case...?

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

17

What can be a variable's value?

Anything

18

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?

[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.

19

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

braces {}, if need be.

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

20

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

unset command.

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

[prince@station prince]$

21

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

The exit status of the most recently executed command.

22

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

Currently enabled shell option flags

23

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

Process id (pid) of current shell.

24

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

Process id (pid) of most recent background command.

25

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

Last token of previous command.

26

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

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

27

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

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

28

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

The userid of the current user.

29

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

The current bash version

30

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

The DNS hostname of the current machine.