Podcast
Questions and Answers
What is the purpose of the backslash character in Bash scripting?
What is the purpose of the backslash character in Bash scripting?
- To print a newline character
- To ignore special meaning of the following character (correct)
- To exit the shell
- To execute a command in the background
By default, a variable is an environment variable.
By default, a variable is an environment variable.
False (B)
What is the purpose of the EDITOR environment variable?
What is the purpose of the EDITOR environment variable?
To specify the default text editor
In Bash scripting, environment variables are commonly named in _______ case.
In Bash scripting, environment variables are commonly named in _______ case.
What is the purpose of the PATH environment variable?
What is the purpose of the PATH environment variable?
Shell variables are used by all processes, not just the shell.
Shell variables are used by all processes, not just the shell.
What is the purpose of the HOME environment variable?
What is the purpose of the HOME environment variable?
Match the following environment variables with their purposes:
Match the following environment variables with their purposes:
What is the command to see all current environment variables and their values?
What is the command to see all current environment variables and their values?
Having a dot (.) in the $PATH is a security risk.
Having a dot (.) in the $PATH is a security risk.
What is the purpose of the export command in bash scripting?
What is the purpose of the export command in bash scripting?
The $HOME variable is set to __________________ by default.
The $HOME variable is set to __________________ by default.
What is the effect of changing the $LANG environment variable?
What is the effect of changing the $LANG environment variable?
The LC_* environment variables are used to set the locale.
The LC_* environment variables are used to set the locale.
Match the environment variables with their descriptions:
Match the environment variables with their descriptions:
What is the command to create an environment variable with a value?
What is the command to create an environment variable with a value?
What is the name of the line that starts with '#!' and specifies the shell or interpreter to use?
What is the name of the line that starts with '#!' and specifies the shell or interpreter to use?
What is the output of the command echo 'Hello $what'
if what
is set to World
?
What is the output of the command echo 'Hello $what'
if what
is set to World
?
The shell interprets the #! line as a comment and executes it.
The shell interprets the #! line as a comment and executes it.
Variable substitution occurs when the variable is used, not when it is assigned.
Variable substitution occurs when the variable is used, not when it is assigned.
What permissions should be set on a script file using the chmod command?
What permissions should be set on a script file using the chmod command?
What is the purpose of double quoting variables in command arguments?
What is the purpose of double quoting variables in command arguments?
To set a variable, use the format: _______________ = VALUE
To set a variable, use the format: _______________ = VALUE
When using variables as command arguments, it is recommended to use ______________ quotes to prevent word splitting and filename expansion.
When using variables as command arguments, it is recommended to use ______________ quotes to prevent word splitting and filename expansion.
What is the output of the command echo $message
if what
is set to There
and message
is set to Hello $what
?
What is the output of the command echo $message
if what
is set to There
and message
is set to Hello $what
?
Match the following terms with their descriptions:
Match the following terms with their descriptions:
Backslashes have a special meaning in shell scripts.
Backslashes have a special meaning in shell scripts.
What is the correct way to execute a script that is not in a directory searched by the operating system?
What is the correct way to execute a script that is not in a directory searched by the operating system?
Match the following concepts with their descriptions:
Match the following concepts with their descriptions:
In variable names, case does not matter.
In variable names, case does not matter.
What is the result of running the command ls -l new file
without quoting?
What is the result of running the command ls -l new file
without quoting?
What characters can variable names contain?
What characters can variable names contain?
How do you access a variable's value in a command?
How do you access a variable's value in a command?
Word splitting is prevented by default in Bash scripting.
Word splitting is prevented by default in Bash scripting.
What is the purpose of quoting in Bash scripting?
What is the purpose of quoting in Bash scripting?
To use a variable anywhere in a command, you can use it as a ______________.
To use a variable anywhere in a command, you can use it as a ______________.
What happens when you use double quotes to quote a string in Bash scripting?
What happens when you use double quotes to quote a string in Bash scripting?
Backslashes are used to quote strings in Bash scripting.
Backslashes are used to quote strings in Bash scripting.
Match the following quote types with their characteristics:
Match the following quote types with their characteristics:
What happens when you use a space in a filename without quoting?
What happens when you use a space in a filename without quoting?
What is the purpose of the read command in Bash scripting?
What is the purpose of the read command in Bash scripting?
If you give read multiple variables, it will tokenize the input.
If you give read multiple variables, it will tokenize the input.
What is the option to use with the read command to display a message to the user?
What is the option to use with the read command to display a message to the user?
The read command is used to read from standard __________________ into a variable.
The read command is used to read from standard __________________ into a variable.
Match the following options with their descriptions:
Match the following options with their descriptions:
The read command can be used to execute a script.
The read command can be used to execute a script.
What is the default behavior of the read command when reading from standard input?
What is the default behavior of the read command when reading from standard input?
What is the purpose of using the echo command with the read command?
What is the purpose of using the echo command with the read command?
What is the purpose of the exit status in a script?
What is the purpose of the exit status in a script?
A shell script exits by calling exit(1) when an error occurs.
A shell script exits by calling exit(1) when an error occurs.
What is the command used to find out the exit status of the last command executed?
What is the command used to find out the exit status of the last command executed?
The if command takes two or more lists of commands, and uses the result of one list to control the execution of the other, which is referred to as ______________ logic.
The if command takes two or more lists of commands, and uses the result of one list to control the execution of the other, which is referred to as ______________ logic.
Match the following components of an if-else statement with their descriptions:
Match the following components of an if-else statement with their descriptions:
What is the purpose of the grep command in the following script: if grep –q "OPS102" testfile then echo "The course is mentioned in the file" fi
What is the purpose of the grep command in the following script: if grep –q "OPS102" testfile then echo "The course is mentioned in the file" fi
A script will execute the commands in the then clause if the exit status of the command is 1.
A script will execute the commands in the then clause if the exit status of the command is 1.
What happens when a command in a script fails?
What happens when a command in a script fails?
What is the purpose of the exit status in a program?
What is the purpose of the exit status in a program?
In Bash scripting, an exit status of zero always means that an error occurred.
In Bash scripting, an exit status of zero always means that an error occurred.
What is the special variable that stores the exit status of the last command?
What is the special variable that stores the exit status of the last command?
In Bash scripting, the exit command is used to exit a script with a status of ______________.
In Bash scripting, the exit command is used to exit a script with a status of ______________.
What is the purpose of the expr command in Bash scripting?
What is the purpose of the expr command in Bash scripting?
Match the following terms with their descriptions:
Match the following terms with their descriptions:
What is the purpose of the elif
keyword in Bash scripting?
What is the purpose of the elif
keyword in Bash scripting?
In Bash scripting, the else
clause is optional in an if
statement.
In Bash scripting, the else
clause is optional in an if
statement.
What is the exit status of a command if it is successful?
What is the exit status of a command if it is successful?
The command lists in an if
statement can be separated by ______________________ or semi-colons.
The command lists in an if
statement can be separated by ______________________ or semi-colons.
What is the purpose of the if
statement in Bash scripting?
What is the purpose of the if
statement in Bash scripting?
Match the following components of an if
statement with their descriptions:
Match the following components of an if
statement with their descriptions:
In Bash scripting, the if
statement is used to handle errors.
In Bash scripting, the if
statement is used to handle errors.
What is the purpose of the fi
keyword in Bash scripting?
What is the purpose of the fi
keyword in Bash scripting?
What is the purpose of the notation $(command) in Bash scripting?
What is the purpose of the notation $(command) in Bash scripting?
Using backticks (reverse single quotes) for command capture is recommended in Bash scripting.
Using backticks (reverse single quotes) for command capture is recommended in Bash scripting.
What is the purpose of shell arithmetic in Bash scripting?
What is the purpose of shell arithmetic in Bash scripting?
The command to capture the standard output of a command as a string is ______________.
The command to capture the standard output of a command as a string is ______________.
Match the following commands with their purposes:
Match the following commands with their purposes:
The read command is used to execute a script.
The read command is used to execute a script.
What is the purpose of the -p option with the read command?
What is the purpose of the -p option with the read command?
Study Notes
Viewing and Creating Environment Variables
- The
printenv
command shows all current environment variables and their values. - The
set
command displays all variables. - To create an environment variable, use the
export
command, for example:X=500
andexport X
. - Environment variables can be created with a single command, for example:
export Y=123
.
PATH Environment Variable
- The
PATH
environment variable is a colon-separated list of directories that will be searched when looking for a command. - The
PATH
variable can be modified to include the current directory, for example:PATH="$PATH:."
. - Having a dot (
.
) in thePATH
is a security risk, but it is the default on some systems.
LANG Environment Variable
- The
LANG
environment variable determines the language and locale settings. - The
LANG
variable can be changed to display dates and times in a different format, for example:LANG=fr_CA.UTF-8
.
HOME Environment Variable
- The
HOME
environment variable specifies the user's home directory. - The
HOME
variable can be modified, but it can cause problems, for example:HOME=/
. - The
~
symbol is an abbreviation for the user's home directory.
Quoting: Backslashes (Escaping)
- A backslash (
\
) character can be used to escape special characters in shell commands. - The
touch
command can be used to create a file with a space in its name, for example:touch "new file"
.
Shell Variables vs Environment Variables
- By default, a variable is local to the shell in which it is running.
- Environment variables are passed to programs (child processes) that are executed by the shell.
- Environment variables are used by all processes, not just the shell.
- By convention, environment variables are named in uppercase.
Common Environment Variables
EDITOR
: specifies the default text editor.PATH
: a colon-separated list of directories that will be searched when looking for a command.LANG
: determines the language and locale settings.HOME
: specifies the user's home directory.
Accessing Variables
- To use a variable value in a command, precede it with a dollar sign (
$
), for example:$VARIABLE
. - The value of the variable is substituted into the command.
Variables: A Simple Example
- Variables can be used to store and display values, for example:
what=World
andecho Hello $what
.
Word Splitting / Tokenization
- The shell uses certain separator characters (whitespace by default) to split commands into words.
- Quoting can be used to prevent word splitting, for example:
ls -l "new file"
.
Preventing Word Splitting with Quoting
- Quoting can be used to hide special characters from the shell, for example:
echo "This string contains a \"quoted\" string"
.
Types of Quotes: Single vs Double
- Single quotes (
'
) and double quotes ("
) can be used to quote strings. - Double quotes allow variable expansion inside the quotes, while single quotes do not.
- The
#
character causes the shell to interpret the line as a comment, and ignore it.
Bash Scripts
- A bash script is a file that contains a series of shell commands.
- The shebang line (
#!/bin/bash
) specifies the interpreter to use to run the script. - The kernel needs execute permission to analyze the shebang line and execute the correct shell/interpreter.
- The
chmod
command can be used to set permissions on the script file, for example:chmod u+rx scriptname
.
Bash Scripting
- Scripts are handy and easy to create, and they use a "shebang" line to guide the kernel on how to execute a script.
- Variables are handy, and they can be local or environment variables.
- Quoting and backslash escaping hide special characters and whitespace.
The Read Command
- The
read
command can read from standard input into a variable. - The
read
command can tokenize the input if given multiple variables. - The
-p
option can be used to display a message to the user when reading from stdin.
Shell Arithmetic
- Bash can do integer arithmetic using
$(( ))
or(( ))
. - Dollar-sign prefixes for variables are not required inside
$(( ))
or(( ))
. - The
expr
command can be used to evaluate expressions, but it's less convenient than bash arithmetic and more portable.
Exit Status and Conditionals
- When a program runs, it exits with a numeric value, also known as the exit status, exit code, status code, or error code.
- An exit status of zero usually means that no errors were encountered, and a non-zero status means that something went wrong.
- The
exit
command can be used to exit a shell script with a specific status code. - The special variable
$?
can be used to find out the exit status of the last command executed.
Conditional Logic
- The
if
command takes two or more lists of commands and uses the result of one list to control the execution of the other. - The
if
command can be used withthen
,elif
, andelse
keywords. - The
if
command can be used to check the exit status of a command and execute different commands based on the status.
Command Capture (Substitution)
- Command capture (substitution) can be used to capture the standard output of a command as a string using the notation
$(command)
. - Command capture is also known as command substitution since the output of the command is substituted for what was on the command line.
- Avoid using backticks for command capture, as it's an archaic syntax that is deprecated and difficult to read.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers viewing and creating environment variables in Bash scripting, including using printenv and export commands.