Week 4 - Bourne Again Shell (Bash) Flashcards

(38 cards)

1
Q

What is Bash?

A

A shell (command interpreter + programming language)

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

What standard does Bash and most Linux/Unix systems adhere to?

A

POSIX standard

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

Bash executes several files before you are in control what are some directories that these files reside in?

Why do we need to execute these files?

A

Login shell:
/etc/profile (global)
~/.bash_login

Non-login shell:
~/.bashrc

Non-login, non interactive:
BASH_ENV

We execute these files to setup the environment settings.

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

What command do we use to redirect standard error to a file?

A

2>
cat x y > hold 2 > errors

(2 = standard error,
1 = standard output,
0 = standard input,
& differentiates from file name)

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

What command do we use to redirect standard error to standard output?

A

2>&1
cat x y > hold 2>&1

(2 = standard error,
1 = standard output,
0 = standard input,
& differentiates from file name)

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

chmod u+x filename

What does u+x do?

A

Gives execution (x) privileges to the owner of the file (u)

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

The first token is always interpreted as a(n) _______

A

Command

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

What does # represent?

A

A shell comment

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

What does #! represent?

A

!/usr/bin/python3

“shebang” defines the interpreter used to run the script

print(“This is a Python script.”)

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

___ separates sequential commands on a line

A

;

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

What are the two uses of &

A

a& runs task in background

> &2 denotes standard error file descriptor instead of filename.

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

Why is a job a command pipeline?

A

Under the hood, a job is multiple commands with pipes:

command1 | command2 | command3

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

fg %jobnumber
kill %jobnumber

what is the % for?

A

% is used to refer to a job number

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

What does CTRL-Z do?

A

Suspends foreground job

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

What is a directory stack?
What command displays the stack?

A

Stores history of visited directories like a navigation stack.

dirs

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

Give an example of a variable

A

name=tommy_j2525

NOTE: No whitespaces and cannot start with a digit

17
Q

What special character do we use to evaluate variables?

A

$

echo $myvar

18
Q

What are some special parameters?

A

$0: The name of the script or command.
$#: The number of arguments passed to the script.
$@: All arguments passed to the script.
$?: The exit status of the last command.
$$ contains the process identification (PID)
$! contains the PID of the last process run in
background

19
Q

What does the shift built-in do?

A

Shifts arguments to the left when you are using positional parameters.

20
Q

What is the difference between a command and a built-in?

A

Commands are external scripts, built-ins are integrated into the shell.

21
Q

What does the set builtin do?

A

assigns arguments to positional parameters

NOTE: set -v (verbose) is useful for debugging

22
Q

What is the output for each? (person=alex)

echo person
echo $person
echo “$person”
echo ‘$person’
echo $person

A

person
alex
alex
person
person

23
Q

What is the output for each?
(person=”alex and jenny”)

echo $person
echo “$person”

A

alex and jenny

alex and jenny

$ directly plops tokens into the command with positional arguments which results in only one space between the words
(echo alex and jenny)

$”” will directly output the original string from the variable
(echo “alex and jenny”)

24
Q

memo=alex*

is
echo “$memo”
the same as echo $memo?

Why?

A

No

echo “$memo” will output alex* (the original string)

echo $memo will replace $memo with alex*
(echo alex*)
which performs string matching
output alex.report alex.summary

25
What does the read builtin do?
Reads a line from the terminal and assigns it to a variable read my_var
26
PATH=/sbin:/usr/sbin:$PATH what does this command do?
An environment variable that specifies where to look for executable files when you run a command. It is a comma-separated list of directories INCLUDING the old path directory (itself)
27
What does the declare built in do?
– -a declares a variable as an array (later) – -f makes the variable a function name – -i marks a variable as an integer – -r marks a variable as readonly – -x marks a variable for export
28
what does pstree -p do?
Shows process tree with PIDs
29
What does ps -Al do?
Shows processes -A shows all processes including those belonging to other users -l long format (more details)
30
What does ps aux do?
shows table or processes using human readable format (Task manager)
31
what does history store?
A history of previously entered commands !! executes last command !n executes command with number n !string executes command starting with string !?string executes command containing string
32
alias ls='ls -color' alias rm='rm -i' What's the difference between single and double quotes? Why do we use single quotes here?
Single quotes ('): Everything inside is taken literally (no variable expansion). Double quotes ("): Variables and escape sequences are evaluated and expanded. We use single quotes because we do not want the command to be expanded and modified. NOTE: Similar to $ which expands variables, '' is similar to $""
33
What is the syntax for a function?
function-name () { commands } NOTE: functions cannot have arguments, and they can overwrite commands with their names!
34
what does the export built in do?
Exports a variable so that it available to child processes or sub-shells.
35
Brace expansion: output of echo page_{one, two, three} ?
page_one page_two page_three NOTE: uses expansion to run the command multiple times with each token.
36
Syntax for evaluating integer expression?
$[expression]
37
How do you perform command substitution?
$(command) or `command` echo "The files in this directory are: $(ls)" NOTE: uses the output of a command as part of another command.
38