Week 4 - Bourne Again Shell (Bash) Flashcards
What is Bash?
A shell (command interpreter + programming language)
What standard does Bash and most Linux/Unix systems adhere to?
POSIX standard
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?
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.
What command do we use to redirect standard error to a file?
2>
cat x y > hold 2 > errors
(2 = standard error,
1 = standard output,
0 = standard input,
& differentiates from file name)
What command do we use to redirect standard error to standard output?
2>&1
cat x y > hold 2>&1
(2 = standard error,
1 = standard output,
0 = standard input,
& differentiates from file name)
chmod u+x filename
What does u+x do?
Gives execution (x) privileges to the owner of the file (u)
The first token is always interpreted as a(n) _______
Command
What does # represent?
A shell comment
What does #! represent?
!/usr/bin/python3
“shebang” defines the interpreter used to run the script
print(“This is a Python script.”)
___ separates sequential commands on a line
;
What are the two uses of &
a& runs task in background
> &2 denotes standard error file descriptor instead of filename.
Why is a job a command pipeline?
Under the hood, a job is multiple commands with pipes:
command1 | command2 | command3
fg %jobnumber
kill %jobnumber
what is the % for?
% is used to refer to a job number
What does CTRL-Z do?
Suspends foreground job
What is a directory stack?
What command displays the stack?
Stores history of visited directories like a navigation stack.
dirs
Give an example of a variable
name=tommy_j2525
NOTE: No whitespaces and cannot start with a digit
What special character do we use to evaluate variables?
$
echo $myvar
What are some special parameters?
$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
What does the shift built-in do?
Shifts arguments to the left when you are using positional parameters.
What is the difference between a command and a built-in?
Commands are external scripts, built-ins are integrated into the shell.
What does the set builtin do?
assigns arguments to positional parameters
NOTE: set -v (verbose) is useful for debugging
What is the output for each? (person=alex)
echo person
echo $person
echo “$person”
echo ‘$person’
echo $person
person
alex
alex
person
person
What is the output for each?
(person=”alex and jenny”)
echo $person
echo “$person”
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”)
memo=alex*
is
echo “$memo”
the same as echo $memo?
Why?
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
What does the read builtin do?
Reads a line from the terminal and assigns it to a variable
read my_var
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)
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
what does pstree -p do?
Shows process tree with PIDs
What does ps -Al do?
Shows processes
-A shows all processes including those belonging to other users
-l long format (more details)
What does ps aux do?
shows table or processes using human readable format (Task manager)
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
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 $””
What is the syntax for a function?
function-name ()
{
commands
}
NOTE: functions cannot have arguments, and they can overwrite commands with their names!
what does the export built in do?
Exports a variable so that it available to child processes or sub-shells.
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.
Syntax for evaluating integer expression?
$[expression]
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.