What does the ? meta-character match?
Any single character
What does the * meta-character match?
Zero or more occurrences of any character
What do the [ ] meta-characters match?
A single instance of any character between the brackets
What command would you use to see all files in your current directory whose name began with "foo", followed by another letter, followed by “txt”?
ls foo?.txt
What do you call a command whose code is part of the shell and does not exist as an executable file on disc?
A built-in
Name the three ways a shell can be started.
login shell, interactive non-login shell, non-interactive shell
What is the name of the startup file that we will be using in this course which contains commands that are run when you first log in?
.bash_profile
Where are the startup files located?
In your home directory
What command would you use the find out more about a built-in command?
help
Are the commands in .bash_profile run when you create a subshell using bash?
No
What is the value of the file descriptor for Standard Input?
0
What is the value of the file descriptor for Standard Output?
1
What is the value of the file descriptor for Standard Error?
2
If you wanted to run the executable file backup in your current directory but wanted the error messages to disappear, what would you write at the command line?
./backup 2> /dev/null
If you wanted to run the executable file backup in your current directory but wanted both the output and the error messages to go to the file results.txt, what would you write at the command line?
./backup &> results.txt
What would you type at the command line if you wanted to make the script cheer.sh executable?
chmod 755 cheer.sh
Write the hashbang line we use for scripts in this course.
#! /bin/bash
Write the hashbang line you would use if you wanted a script to run using the sh shell.
#! /bin/sh
Name the three categories of people who might need to read a shell script.
The person who wrote the script, the persons who use the script, and the person who maintains the script
What command would you use to make the changes you made in the startup file for your login shell take effect?
source .bash_profile
What is special about a global variable?
You can use it in any subshell of the shell in which it was created.
What are the limitations of a local variable?
You can only use it in the shell in which it was created
What would you write on the command line to set the value of the local variable named team to Red Sox.
team="Red Sox"
If you define a local variable, can a script see the value of this variable?
No
What would you write on the command line to create the global variable named school to UMass Boston.
export school="UMass Boston"
Write the command you would use in a script to ask the user to provide a value for a variable named dir.
read -p "Directory: " dir
What command would you use to see all global variables?
env
What character would you use if you wanted to run two commands in the foreground, one right after the other, on a single command line?
;
What other two characters allow more than one command to be run on a single command line?
& and |
How can you continue a command onto the next line?
type \ immediately before hitting Enter
What parameter gives the status code returned by the last command run?
?
What parameter gives the total number of arguments passed to a script from the command line?
#
What parameter gives the full pathname of the script that is currently being run?
0
What parameter gives the first command line argument to a script or function?
1
If you wanted to use the value of the variable team inside quotes, what kind of quotes would you use?
Double quotes
If you wanted to remove the value of the variable team, what would you type at the command line?
team = or unset team
What is the name of the first process created when the machine starts?
init or systemd
Can two processes running at the same time have the same process ID number?
No
Can a process ID number be reused after the process has finished?
Yes
If the process ID of your current shell is 15062, what is the parent process ID of a shell script you run from your current shell?
15062
What command would you run to see the last 20 commands you ran at the command line?
history 20
What is the pathname of the file that, by default, stores the commands you have run in a file so that they will be remembered for the next time you log in?
~/.bash_history
What command would you use to edit the last command to run, change it and run it again?
fc
What is the name of the library of procedures that allows you to edit the command line?
The Readline library
Name the three things that can be completed at the command line by hitting Tab.
pathnames, commands, shell variables
What do you call a shortcut that is used to run a command?
an alias
What would you write at the command line to create the alias ghdir which changed directory to my home directory?
alias ghdir='cd ~ghoffman'
Can you write a function on a single command line?
Yes if every command is followed by a ; including the last
Can aliases be made global?
No
Can functions be made global?
No
What does Bash do when it performs history expansion?
It reruns an old command line which it retrieves from the history list
What does Bash do when it performs alias substitution?
It replaces the name of the alias with a Unix command
Write the command you would use to create the files foo1.txt to foo5.txt using brace expansion and the touch command.
touch foo{1,2,3,4,5}.txt
What does Bash do when it performs tilde (~) expansion?
Replaces ~ with the absolute address of your home directory or replaces ~UNIX_USERNAME with the absolute address of the home directory of UNIX_USERNAME
What does Bash do when it performs parameter and variable expansion?
substitutes the value of a variable or a parameter for the name of the variable or parameter when preceded by a $
What does Bash do when it performs arithmetic expansion?
Treats the digits inside double parentheses as numbers performs arithmetic operations on them and substitutes the result of that calculation for the expansion expression
What does Bash do when it performs command substitution?
Runs the commands inside the parentheses in a subshell and substitutes the output from those commands for the substitution expression
What would you write at the command line to define the alias ll whose value was ls -l ?
alias ll='ls -l'
Can an alias be made global?
No
Can a function be made global?
No
What are control structures?
Syntactic structures that control how and when other statements are executed
What are the two basic types of control structures?
Conditionals and loops
What is the basic format of the if ... then construct?
if COMMAND
Then
COMMAND_1
COMMAND_2
…
fi
How is the command that follows if used to determine whether or not the statements between the then and fi keywords are executed?
If the command returns an exit status of 0 then statements are executed
What does an exit status of 0 mean?
That the command that returned the status code ran without error
What are the two keywords that surround the statements inside an if statement?
then and fi
Can the then keyword appear on the same line as if?
Yes, but only if it is separated from it by a semicolon (;)
What does the test command do?
Analyze a logical expression and return an exit status that tells whether the expression is true or false
What is the value of the exit status returned by test to indicate that the expression is true?
0
Does the command that follows “if” need to be tested?
No, it can be any command that returns an exit status
What do [ and ] mean when used in an if statement?
They stand for the test command
What are the keywords used to mark a block of commands in an if ... then statement?
then and fi
What are the keywords used to mark the first block of commands in an if ... then ... else statement?
then and else
What are the keywords used to mark the second block of commands in an if ... then ... else statement?
else and fi
What are the keywords used to mark the block of commands following elif?
then and either else or elif or fi
Is there any limit to the number of blocks of commands you can have in an if ... else ... elif statement?
No
How would you run the script do_something.sh if you wanted to print each line of the script before it was executed?
bash -x do_something.sh
If you were debugging a script as in the example above, how would Bash mark the lines of the script itself to distinguish them from the output of the script?
By starting each line with a +
What has to follow each elif in an if ... else ... elif statement?
A Unix command
How does the test command indicate that the logical expression it is evaluating is true?
By returning a status code of 0
In a for ... in loop, where does the loop variable get its values?
From the list of values following the keyword in
In a simple for loop, where does the loop variable get its values?
From the command line arguments
Should the loop variable in a for loop have a $ in front of it?
No
In a three statement for loop, what does the first statement do?
Sets the initial value of a loop variable
In a three statement for loop, what does the second statement do?
It's a logical expression and the loop will continue as long as its value is true
In a three statement for loop, what does the third statement do?
changes the value of the loop variable after each pass through the loop
Why are the three statements in a three statement for loop surrounded by two parentheses?
So the values of all variables will be treated as numbers
When does a while loop stop running?
When the command following while returns an exit status that is not 0
What does the continue command in a loop do?
Stops that pass through the loop and returns to the top of the loop
What does the break command in a loop do?
Jumps completely out of the loop
In a case statement, what determines which block of code is executed?
The value of the variable between case and in
How do you mark the end of a code block in a case statement?
;;
What signals the end of a pattern in a case statement?
)
What meta-characters can you use in a case statement pattern?
all of them: ?, * and [ ]
What pattern would you use in a case statement if you wanted to match 11, 12 or 13?
11|12|13
What will you see on the screen if you ran a script that uses the select statement?
A menu of numbers followed by values
What happens to the value chosen by the user when responding to a select statement?
The value is assigned to the select variable
What function does the PS3 keyword shell variable serve in a select statement?
It contains the text used in the menu prompt
How do you mark the beginning of a here document in a shell script?
<< followed immediately by a string with no spaces between them
How do you mark the end of a here document?
With the same string that starts the here document on a line by itself