Bird
Raised Fist0
Bash Scriptingscripting~10 mins

First Bash script in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print 'Hello, World!' in Bash.

Bash Scripting
echo [1]
Drag options to blanks, or click blank then click option'
AHello World
BHello, World!
C'Hello World'
D"Hello, World!"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes Bash to treat words as separate arguments.
Using single quotes without proper spacing changes the output.
2fill in blank
medium

Complete the code to assign the value '5' to a variable named count in Bash.

Bash Scripting
count=[1]
Drag options to blanks, or click blank then click option'
A5
B"5"
C'5'
Dfive
Attempts:
3 left
💡 Hint
Common Mistakes
Adding spaces around '=' causes errors.
Using words instead of numbers changes the variable value.
3fill in blank
hard

Fix the error in the code to print the value of the variable name in Bash.

Bash Scripting
echo [1]
Drag options to blanks, or click blank then click option'
A$name
Bname
C"name"
D'$name'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the variable name without $ prints the literal text.
Using single quotes prevents variable expansion.
4fill in blank
hard

Fill both blanks to create a loop that prints numbers 1 to 5 in Bash.

Bash Scripting
for i in [1]
do
  echo [2]
done
Drag options to blanks, or click blank then click option'
A"{1..5}"
Bi
C$i
D1 2 3 4 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the list makes it a single string.
Not using $ before the variable prints the variable name.
5fill in blank
hard

Fill all three blanks to create a conditional that prints 'Even' for even numbers in Bash.

Bash Scripting
if [ $(( [1] % [2] )) [3] 0 ]; then
  echo "Even"
fi
Drag options to blanks, or click blank then click option'
Anum
B2
C-eq
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '-eq' causes errors in numeric tests.
Using wrong variable names or missing $ in arithmetic.

Practice

(1/5)
1. What is the purpose of the first line #!/bin/bash in a Bash script?
easy
A. It comments out the first line so it is ignored.
B. It prints a welcome message when the script runs.
C. It sets the script to run in the background automatically.
D. It tells the system to use the Bash shell to run the script.

Solution

  1. Step 1: Understand the shebang line

    The line #!/bin/bash is called a shebang and tells the system which interpreter to use.
  2. Step 2: Identify its role in script execution

    This line ensures the script runs with the Bash shell, not another shell or program.
  3. Final Answer:

    It tells the system to use the Bash shell to run the script. -> Option D
  4. Quick Check:

    Shebang = Bash interpreter [OK]
Hint: Shebang line always points to the shell interpreter [OK]
Common Mistakes:
  • Thinking it prints a message
  • Confusing it with a comment
  • Assuming it runs script in background
2. Which of the following is the correct way to print "Hello, World!" in a Bash script?
easy
A. echo "Hello, World!"
B. printf(Hello, World!)
C. console.log("Hello, World!")
D. print("Hello, World!")

Solution

  1. Step 1: Identify the Bash command for output

    In Bash, echo is used to print text to the screen.
  2. Step 2: Check syntax correctness

    echo "Hello, World!" correctly prints the message with quotes around the string.
  3. Final Answer:

    echo "Hello, World!" -> Option A
  4. Quick Check:

    Print command in Bash = echo [OK]
Hint: Use echo to print text in Bash scripts [OK]
Common Mistakes:
  • Using print() like in Python
  • Using console.log() from JavaScript
  • Missing quotes in printf
3. What will be the output of this Bash script?
#!/bin/bash
echo "Start"
echo "End"
medium
A. Start\nEnd
B. Start End (two lines)
C. Start End
D. Start End

Solution

  1. Step 1: Understand echo behavior

    The echo command prints text followed by a new line by default.
  2. Step 2: Analyze the script output

    Two echo commands print "Start" and "End" each on its own line.
  3. Final Answer:

    Start End (two lines) -> Option B
  4. Quick Check:

    echo adds newline = two lines output [OK]
Hint: Each echo prints on a new line by default [OK]
Common Mistakes:
  • Thinking echo prints on same line
  • Confusing \n as literal text
  • Ignoring new line after echo
4. Identify the error in this Bash script:
#!/bin/bash
echo Hello World
medium
A. Missing quotes around Hello World
B. echo command is misspelled
C. No error, script runs fine
D. Missing shebang line

Solution

  1. Step 1: Check script syntax

    The script has a correct shebang line and uses echo properly.
  2. Step 2: Understand echo argument rules

    Quotes are optional if no special characters or spaces that confuse shell; here it works fine.
  3. Final Answer:

    No error, script runs fine -> Option C
  4. Quick Check:

    echo without quotes works here [OK]
Hint: Quotes optional if no special chars or spaces [OK]
Common Mistakes:
  • Assuming quotes are always required
  • Thinking shebang is missing
  • Believing echo is misspelled
5. You want to write a Bash script that prints your name and then the current date. Which script below will do this correctly?
hard
A. #!/bin/bash echo "My name is Alice" date
B. #!/bin/bash print "My name is Alice" date
C. #!/bin/bash echo My name is Alice current_date
D. #!/bin/bash echo "My name is Alice"; date %Y-%m-%d

Solution

  1. Step 1: Check commands for printing text and date

    echo prints text; date prints current date and time.
  2. Step 2: Verify script correctness

    #!/bin/bash echo "My name is Alice" date uses echo correctly and calls date without errors.
  3. Final Answer:

    #!/bin/bash\necho "My name is Alice"\ndate -> Option A
  4. Quick Check:

    Use echo and date commands correctly [OK]
Hint: Use echo for text and date command for current date [OK]
Common Mistakes:
  • Using print instead of echo
  • Calling undefined commands like current_date
  • Incorrect date command syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard