OPS102 W8C1 Bash Scripting: Environment Variables
77 Questions
7 Views

OPS102 W8C1 Bash Scripting: Environment Variables

Created by
@VibrantSwamp

Questions and Answers

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.

    False

    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.

    <p>UPPERCASE</p> Signup and view all the answers

    What is the purpose of the PATH environment variable?

    <p>To specify a list of directories to search for commands</p> Signup and view all the answers

    Shell variables are used by all processes, not just the shell.

    <p>False</p> Signup and view all the answers

    What is the purpose of the HOME environment variable?

    <p>To specify the user's home directory</p> Signup and view all the answers

    Match the following environment variables with their purposes:

    <p>EDITOR = Specify the default text editor PATH = Specify a list of directories to search for commands LANG = Specify the default language HOME = Specify the user's home directory</p> Signup and view all the answers

    What is the command to see all current environment variables and their values?

    <p>printenv</p> Signup and view all the answers

    Having a dot (.) in the $PATH is a security risk.

    <p>True</p> Signup and view all the answers

    What is the purpose of the export command in bash scripting?

    <p>To create an environment variable</p> Signup and view all the answers

    The $HOME variable is set to __________________ by default.

    <p>/home/chris</p> Signup and view all the answers

    What is the effect of changing the $LANG environment variable?

    <p>Changes the language of the date command</p> Signup and view all the answers

    The LC_* environment variables are used to set the locale.

    <p>True</p> Signup and view all the answers

    Match the environment variables with their descriptions:

    <p>PATH = Specifies the directories to search for executables LANG = Specifies the language and locale HOME = Specifies the user's home directory</p> Signup and view all the answers

    What is the command to create an environment variable with a value?

    <p>export X=500</p> Signup and view all the answers

    What is the name of the line that starts with '#!' and specifies the shell or interpreter to use?

    <p>Shebang line</p> Signup and view all the answers

    What is the output of the command echo 'Hello $what' if what is set to World?

    <p>Hello $what</p> Signup and view all the answers

    The shell interprets the #! line as a comment and executes it.

    <p>False</p> Signup and view all the answers

    Variable substitution occurs when the variable is used, not when it is assigned.

    <p>False</p> Signup and view all the answers

    What permissions should be set on a script file using the chmod command?

    <p>execute permission for the kernel and read permission for the shell</p> Signup and view all the answers

    What is the purpose of double quoting variables in command arguments?

    <p>To prevent the shell from interpreting the variable value as multiple words</p> Signup and view all the answers

    To set a variable, use the format: _______________ = VALUE

    <p>VARIABLE</p> 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.

    <p>double</p> 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?

    <p>Hello World</p> Signup and view all the answers

    Match the following terms with their descriptions:

    <p>Shebang line = Specifies the shell or interpreter to use Hashbang line = Alternative name for the shebang line Magic number = The #! characters that the kernel notices Variable = A named storage location for a value</p> Signup and view all the answers

    Backslashes have a special meaning in shell scripts.

    <p>False</p> 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?

    <p>Use a pathname that includes a slash to execute the script</p> Signup and view all the answers

    Match the following concepts with their descriptions:

    <p>Variable Substitution = The process of replacing a variable with its value Quoting = The process of using quotes to prevent word splitting and filename expansion Environment Variables = Not mentioned in the provided content Shell Variables = Not mentioned in the provided content</p> Signup and view all the answers

    In variable names, case does not matter.

    <p>False</p> Signup and view all the answers

    What is the result of running the command ls -l new file without quoting?

    <p>The shell interprets <code>new</code> and <code>file</code> as separate arguments.</p> Signup and view all the answers

    What characters can variable names contain?

    <p>letters, numbers, and underscores</p> Signup and view all the answers

    How do you access a variable's value in a command?

    <p>Precede it with a dollar sign</p> Signup and view all the answers

    Word splitting is prevented by default in Bash scripting.

    <p>False</p> Signup and view all the answers

    What is the purpose of quoting in Bash scripting?

    <p>To prevent word splitting and hide special characters</p> Signup and view all the answers

    To use a variable anywhere in a command, you can use it as a ______________.

    <p>command name or argument</p> Signup and view all the answers

    What happens when you use double quotes to quote a string in Bash scripting?

    <p>Variables will be expanded inside the quotes</p> Signup and view all the answers

    Backslashes are used to quote strings in Bash scripting.

    <p>False</p> Signup and view all the answers

    Match the following quote types with their characteristics:

    <p>Single quotes = Variables will not be expanded inside the quotes Double quotes = Variables will be expanded inside the quotes</p> Signup and view all the answers

    What happens when you use a space in a filename without quoting?

    <p>It will be interpreted as multiple arguments</p> Signup and view all the answers

    What is the purpose of the read command in Bash scripting?

    <p>To read from standard input into a variable</p> Signup and view all the answers

    If you give read multiple variables, it will tokenize the input.

    <p>True</p> Signup and view all the answers

    What is the option to use with the read command to display a message to the user?

    <p>-p</p> Signup and view all the answers

    The read command is used to read from standard __________________ into a variable.

    <p>input</p> Signup and view all the answers

    Match the following options with their descriptions:

    <p>-p = Display a message to the user -n = Suppress the trailing newline read = Execute a script echo = Read from standard input</p> Signup and view all the answers

    The read command can be used to execute a script.

    <p>False</p> Signup and view all the answers

    What is the default behavior of the read command when reading from standard input?

    <p>It will tokenize the input</p> Signup and view all the answers

    What is the purpose of using the echo command with the read command?

    <p>To display a message to the user</p> Signup and view all the answers

    What is the purpose of the exit status in a script?

    <p>To notice if a command fails</p> Signup and view all the answers

    A shell script exits by calling exit(1) when an error occurs.

    <p>False</p> Signup and view all the answers

    What is the command used to find out the exit status of the last command executed?

    <p>echo $?</p> 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.

    <p>conditional</p> Signup and view all the answers

    Match the following components of an if-else statement with their descriptions:

    <p>if = Specifies the condition to be checked then = Specifies the commands to be executed if the condition is true elif = Specifies an alternative condition to be checked else = Specifies the commands to be executed if the condition is false fi = Specifies the end of the if-else statement</p> 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

    <p>To search for a pattern in a file</p> 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.

    <p>False</p> Signup and view all the answers

    What happens when a command in a script fails?

    <p>The script will exit with a non-zero exit status.</p> Signup and view all the answers

    What is the purpose of the exit status in a program?

    <p>To indicate whether an error occurred</p> Signup and view all the answers

    In Bash scripting, an exit status of zero always means that an error occurred.

    <p>False</p> Signup and view all the answers

    What is the special variable that stores the exit status of the last command?

    <p>$?</p> Signup and view all the answers

    In Bash scripting, the exit command is used to exit a script with a status of ______________.

    <p>0</p> Signup and view all the answers

    What is the purpose of the expr command in Bash scripting?

    <p>To evaluate arithmetic expressions</p> Signup and view all the answers

    Match the following terms with their descriptions:

    <p>Exit Status = A numeric value that a program returns when it finishes executing Exit Code = Another name for the exit status Status Code = Another name for the exit status Error Code = Another name for the exit status</p> Signup and view all the answers

    What is the purpose of the elif keyword in Bash scripting?

    <p>To execute a command only if the previous command has failed</p> Signup and view all the answers

    In Bash scripting, the else clause is optional in an if statement.

    <p>True</p> Signup and view all the answers

    What is the exit status of a command if it is successful?

    <p>0</p> Signup and view all the answers

    The command lists in an if statement can be separated by ______________________ or semi-colons.

    <p>newlines</p> Signup and view all the answers

    What is the purpose of the if statement in Bash scripting?

    <p>To execute a command only if a condition is true</p> Signup and view all the answers

    Match the following components of an if statement with their descriptions:

    <p><code>if</code> = Specifies the condition to be evaluated <code>then</code> = Specifies the command to be executed if the condition is true <code>elif</code> = Specifies an alternative condition to be evaluated <code>else</code> = Specifies the command to be executed if the condition is false</p> Signup and view all the answers

    In Bash scripting, the if statement is used to handle errors.

    <p>False</p> Signup and view all the answers

    What is the purpose of the fi keyword in Bash scripting?

    <p>To mark the end of an <code>if</code> statement</p> Signup and view all the answers

    What is the purpose of the notation $(command) in Bash scripting?

    <p>To capture the standard output of a command as a string</p> Signup and view all the answers

    Using backticks (reverse single quotes) for command capture is recommended in Bash scripting.

    <p>False</p> Signup and view all the answers

    What is the purpose of shell arithmetic in Bash scripting?

    <p>To perform arithmetic operations</p> Signup and view all the answers

    The command to capture the standard output of a command as a string is ______________.

    <p>$(command)</p> Signup and view all the answers

    Match the following commands with their purposes:

    <p>read = To read input from the user and store it in a variable echo = To display output to the screen $(command) = To capture the standard output of a command as a string</p> Signup and view all the answers

    The read command is used to execute a script.

    <p>False</p> Signup and view all the answers

    What is the purpose of the -p option with the read command?

    <p>To display a message to the user</p> 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 and export 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 the PATH 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 and echo 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 with then, elif, and else 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.

    Quiz Team

    Description

    This quiz covers viewing and creating environment variables in Bash scripting, including using printenv and export commands.

    More Quizzes Like This

    Bash Scripting Basics
    10 questions

    Bash Scripting Basics

    SoulfulObsidian avatar
    SoulfulObsidian
    Bash Scripting
    12 questions

    Bash Scripting

    ZippyUvarovite avatar
    ZippyUvarovite
    Bash Shell Scripting Parameters Quiz
    15 questions
    Bash Shell Scripting Basics
    18 questions

    Bash Shell Scripting Basics

    ConfidentTechnetium avatar
    ConfidentTechnetium
    Use Quizgecko on...
    Browser
    Browser