Week 4 - Bourne Again Shell (Bash) Flashcards

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
Q

What does the read builtin do?

A

Reads a line from the terminal and assigns it to a variable

read my_var

26
Q

PATH=/sbin:/usr/sbin:$PATH

what does this command do?

A

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
Q

What does the declare built in do?

A

– -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
Q

what does pstree -p do?

A

Shows process tree with PIDs

29
Q

What does ps -Al do?

A

Shows processes

-A shows all processes including those belonging to other users

-l long format (more details)

30
Q

What does ps aux do?

A

shows table or processes using human readable format (Task manager)

31
Q

what does history store?

A

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
Q

alias ls=’ls -color’
alias rm=’rm -i’

What’s the difference between single and double quotes? Why do we use single quotes here?

A

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
Q

What is the syntax for a function?

A

function-name ()
{
commands
}

NOTE: functions cannot have arguments, and they can overwrite commands with their names!

34
Q

what does the export built in do?

A

Exports a variable so that it available to child processes or sub-shells.

35
Q

Brace expansion:

output of echo page_{one, two, three} ?

A

page_one page_two page_three

NOTE: uses expansion to run the command multiple times with each token.

36
Q

Syntax for evaluating integer expression?

A

$[expression]

37
Q

How do you perform command substitution?

A

$(command) or command

echo “The files in this directory are: $(ls)”

NOTE: uses the output of a command as part of another command.