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?
By default, a variable is an environment variable.
By default, a variable is an environment variable.
False
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.
Signup and view all the answers
What is the purpose of the PATH environment variable?
What is the purpose of the PATH environment variable?
Signup and view all the answers
Shell variables are used by all processes, not just the shell.
Shell variables are used by all processes, not just the shell.
Signup and view all the answers
What is the purpose of the HOME environment variable?
What is the purpose of the HOME environment variable?
Signup and view all the answers
Match the following environment variables with their purposes:
Match the following environment variables with their purposes:
Signup and view all the answers
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?
Signup and view all the answers
Having a dot (.) in the $PATH is a security risk.
Having a dot (.) in the $PATH is a security risk.
Signup and view all the answers
What is the purpose of the export command in bash scripting?
What is the purpose of the export command in bash scripting?
Signup and view all the answers
The $HOME variable is set to __________________ by default.
The $HOME variable is set to __________________ by default.
Signup and view all the answers
What is the effect of changing the $LANG environment variable?
What is the effect of changing the $LANG environment variable?
Signup and view all the answers
The LC_* environment variables are used to set the locale.
The LC_* environment variables are used to set the locale.
Signup and view all the answers
Match the environment variables with their descriptions:
Match the environment variables with their descriptions:
Signup and view all the answers
What is the command to create an environment variable with a value?
What is the command to create an environment variable with a value?
Signup and view all the answers
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?
Signup and view all the answers
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
?
Signup and view all the answers
The shell interprets the #! line as a comment and executes it.
The shell interprets the #! line as a comment and executes it.
Signup and view all the answers
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.
Signup and view all the answers
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?
Signup and view all the answers
What is the purpose of double quoting variables in command arguments?
What is the purpose of double quoting variables in command arguments?
Signup and view all the answers
To set a variable, use the format: _______________ = VALUE
To set a variable, use the format: _______________ = VALUE
Signup and view all the answers
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.
Signup and view all the answers
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
?
Signup and view all the answers
Match the following terms with their descriptions:
Match the following terms with their descriptions:
Signup and view all the answers
Backslashes have a special meaning in shell scripts.
Backslashes have a special meaning in shell scripts.
Signup and view all the answers
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?
Signup and view all the answers
Match the following concepts with their descriptions:
Match the following concepts with their descriptions:
Signup and view all the answers
In variable names, case does not matter.
In variable names, case does not matter.
Signup and view all the answers
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?
Signup and view all the answers
What characters can variable names contain?
What characters can variable names contain?
Signup and view all the answers
How do you access a variable's value in a command?
How do you access a variable's value in a command?
Signup and view all the answers
Word splitting is prevented by default in Bash scripting.
Word splitting is prevented by default in Bash scripting.
Signup and view all the answers
What is the purpose of quoting in Bash scripting?
What is the purpose of quoting in Bash scripting?
Signup and view all the answers
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 ______________.
Signup and view all the answers
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?
Signup and view all the answers
Backslashes are used to quote strings in Bash scripting.
Backslashes are used to quote strings in Bash scripting.
Signup and view all the answers
Match the following quote types with their characteristics:
Match the following quote types with their characteristics:
Signup and view all the answers
What happens when you use a space in a filename without quoting?
What happens when you use a space in a filename without quoting?
Signup and view all the answers
What is the purpose of the read command in Bash scripting?
What is the purpose of the read command in Bash scripting?
Signup and view all the answers
If you give read multiple variables, it will tokenize the input.
If you give read multiple variables, it will tokenize the input.
Signup and view all the answers
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?
Signup and view all the answers
The read command is used to read from standard __________________ into a variable.
The read command is used to read from standard __________________ into a variable.
Signup and view all the answers
Match the following options with their descriptions:
Match the following options with their descriptions:
Signup and view all the answers
The read command can be used to execute a script.
The read command can be used to execute a script.
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
What is the purpose of the exit status in a script?
What is the purpose of the exit status in a script?
Signup and view all the answers
A shell script exits by calling exit(1) when an error occurs.
A shell script exits by calling exit(1) when an error occurs.
Signup and view all the answers
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?
Signup and view all the answers
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.
Signup and view all the answers
Match the following components of an if-else statement with their descriptions:
Match the following components of an if-else statement with their descriptions:
Signup and view all the answers
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
Signup and view all the answers
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.
Signup and view all the answers
What happens when a command in a script fails?
What happens when a command in a script fails?
Signup and view all the answers
What is the purpose of the exit status in a program?
What is the purpose of the exit status in a program?
Signup and view all the answers
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.
Signup and view all the answers
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?
Signup and view all the answers
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 ______________.
Signup and view all the answers
What is the purpose of the expr command in Bash scripting?
What is the purpose of the expr command in Bash scripting?
Signup and view all the answers
Match the following terms with their descriptions:
Match the following terms with their descriptions:
Signup and view all the answers
What is the purpose of the elif
keyword in Bash scripting?
What is the purpose of the elif
keyword in Bash scripting?
Signup and view all the answers
In Bash scripting, the else
clause is optional in an if
statement.
In Bash scripting, the else
clause is optional in an if
statement.
Signup and view all the answers
What is the exit status of a command if it is successful?
What is the exit status of a command if it is successful?
Signup and view all the answers
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.
Signup and view all the answers
What is the purpose of the if
statement in Bash scripting?
What is the purpose of the if
statement in Bash scripting?
Signup and view all the answers
Match the following components of an if
statement with their descriptions:
Match the following components of an if
statement with their descriptions:
Signup and view all the answers
In Bash scripting, the if
statement is used to handle errors.
In Bash scripting, the if
statement is used to handle errors.
Signup and view all the answers
What is the purpose of the fi
keyword in Bash scripting?
What is the purpose of the fi
keyword in Bash scripting?
Signup and view all the answers
What is the purpose of the notation $(command) in Bash scripting?
What is the purpose of the notation $(command) in Bash scripting?
Signup and view all the answers
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.
Signup and view all the answers
What is the purpose of shell arithmetic in Bash scripting?
What is the purpose of shell arithmetic in Bash scripting?
Signup and view all the answers
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 ______________.
Signup and view all the answers
Match the following commands with their purposes:
Match the following commands with their purposes:
Signup and view all the answers
The read command is used to execute a script.
The read command is used to execute a script.
Signup and view all the answers
What is the purpose of the -p option with the read command?
What is the purpose of the -p option with the read command?
Signup and view all the answers
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.