Bash Variables, Loops, and Functions Essentials Quiz Quiz

Explore your understanding of Bash variables, loop structures, and functions, crucial elements in the tools ecosystem for creating effective CLI utilities. Enhance your skills by identifying best practices, syntax, and common pitfalls in Bash scripting related to these core components.

  1. Variable Assignment Syntax

    Which line correctly assigns the string value 'world' to the variable GREETING in a Bash script?

    1. GREETING=world
    2. GREETING == 'world'
    3. GREETING: world
    4. var GREETING = 'world'

    Explanation: In Bash, variable assignment requires no spaces around the equals sign, thus 'GREETING=world' is correct. The option 'GREETING == 'world'' uses a comparison operator which is incorrect for assignment. 'GREETING: world' is not a valid assignment syntax in Bash. 'var GREETING = 'world'' follows syntax from other programming languages but is invalid in Bash.

  2. Loop Iteration Over File List

    How would you iterate over each filename in the current directory using a Bash for loop?

    1. for file in *; do echo $file; done
    2. for file of * { echo $file }
    3. foreach file [*]; print $file; end
    4. loop file from *: print($file)

    Explanation: The syntax 'for file in *; do echo $file; done' is the correct Bash method for looping through filenames. 'for file of * { echo $file }' uses incorrect syntax and braces. 'foreach file [*]; print $file; end' is not recognized in Bash, though similar in other shells. 'loop file from *: print($file)' uses invalid keywords and syntax for Bash.

  3. Function Declaration

    Which statement properly defines a function named 'showDate' that displays the current date in Bash?

    1. showDate() { date; }
    2. def showDate() { date }
    3. function showDate: { date }
    4. showDate = function() { date; }

    Explanation: The correct way in Bash is 'showDate() { date; }' which defines the function and its command block. 'def showDate() { date }' and 'function showDate: { date }' use keywords or symbols not used in Bash. 'showDate = function() { date; }' mixes up styles from other programming languages and is not valid Bash syntax.

  4. Passing Arguments to Functions

    In Bash scripting, how can a function named 'printMsg' access its first argument when called as 'printMsg Hello'?

    1. By using $1 inside the function
    2. By accessing arg1 inside the function
    3. By using $msg inside the function
    4. By referencing function[1] inside the function

    Explanation: The special variable '$1' inside a Bash function refers to the first positional argument given to the function. 'arg1' and 'msg' are not automatically set in Bash functions. 'function[1]' is not a valid way to access arguments in Bash. Only '$1', '$2', etc., are used for function arguments.

  5. Loop Control

    If you want to immediately skip to the next iteration in a Bash 'while' loop upon a certain condition, which keyword should you use?

    1. continue
    2. exit
    3. skip
    4. next

    Explanation: The 'continue' keyword tells the Bash loop to skip all remaining commands in the current iteration and proceed to the next cycle. Using 'exit' will terminate the entire script, not just the current loop iteration. There are no 'skip' or 'next' keywords in Bash for this purpose, making them invalid.