Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why Bash scripting automates Linux tasks - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bash Automation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script?
Consider this Bash script that automates a simple task of listing files and counting them:
Bash Scripting
#!/bin/bash
count=$(ls | wc -l)
echo "Number of files: $count"
ASyntax error near unexpected token `|'
BNumber of files: 0
Cls: command not found
DNumber of files: <actual number of files in current directory>
Attempts:
2 left
💡 Hint
The script counts files in the current directory using ls and wc commands.
🧠 Conceptual
intermediate
1:30remaining
Why is Bash scripting useful for Linux task automation?
Which of the following best explains why Bash scripting is commonly used to automate Linux tasks?
ABecause Bash scripts run faster than compiled programs
BBecause Bash scripts can combine multiple Linux commands easily to automate workflows
CBecause Bash scripts require no permissions to run on any system
DBecause Bash scripts can only be run on Windows systems
Attempts:
2 left
💡 Hint
Think about how Bash scripts use existing Linux commands.
🔧 Debug
advanced
2:00remaining
What error does this Bash script produce?
Look at this script intended to automate file backup but it has a bug:
Bash Scripting
#!/bin/bash
backup_dir=/backup
mkdir -p $backup_dir
cp /home/user/*.txt $backup_dir
ASyntax error: unexpected end of file
Bcp: cannot stat '/home/user/*.txt': No such file or directory
Cmkdir: cannot create directory ‘/backup’: Permission denied
DNo error, script runs successfully
Attempts:
2 left
💡 Hint
Consider permissions when creating directories outside your home folder.
🚀 Application
advanced
2:30remaining
Which script snippet correctly automates checking if a service is running and restarts it if not?
You want to automate checking if 'nginx' service is active and restart it if it is not. Which snippet does this correctly?
Aif systemctl is-active --quiet nginx; then echo 'Running'; else systemctl restart nginx; fi
Bif systemctl is-active nginx; then systemctl restart nginx; else echo 'Running'; fi
Csystemctl restart nginx if not systemctl is-active nginx
Dif [ $(systemctl is-active nginx) = 'inactive' ]; then echo 'Running'; fi
Attempts:
2 left
💡 Hint
Check the correct use of systemctl is-active with --quiet and conditional logic.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using Bash scripting over manual command execution for Linux task automation?
Choose the best explanation for why Bash scripting is preferred for automating Linux tasks instead of typing commands manually each time.
ABash scripts can be scheduled to run automatically and repeat tasks without human intervention
BBash scripts always run faster than manual commands typed in terminal
CBash scripts can only be run by system administrators
DBash scripts do not require any knowledge of Linux commands
Attempts:
2 left
💡 Hint
Think about automation and repetition benefits.

Practice

(1/5)
1. Why do people use Bash scripting to automate Linux tasks?
easy
A. To make the computer run slower
B. To save time and reduce mistakes by repeating commands automatically
C. To avoid using the terminal completely
D. To create graphical user interfaces easily

Solution

  1. Step 1: Understand the purpose of automation

    Automation helps by running tasks repeatedly without manual input, saving time and avoiding errors.
  2. Step 2: Relate Bash scripting to automation

    Bash scripts contain commands that run in order, automating tasks on Linux systems.
  3. Final Answer:

    To save time and reduce mistakes by repeating commands automatically -> Option B
  4. Quick Check:

    Automation = save time and reduce errors [OK]
Hint: Automation means repeating tasks to save time [OK]
Common Mistakes:
  • Thinking Bash slows down the computer
  • Believing Bash replaces the terminal
  • Confusing Bash scripting with GUI design
2. Which line correctly starts a Bash script to tell the system to use Bash?
easy
A. #!/bin/bash
B. #!/bin/sh
C. #!/usr/bin/python
D. #!/bin/csh

Solution

  1. Step 1: Identify the correct shebang for Bash

    The shebang line #!/bin/bash tells the system to run the script with Bash shell.
  2. Step 2: Compare other options

    #!/bin/sh is for sh shell, #!/usr/bin/python is for Python, and #!/bin/csh is for C shell, not Bash.
  3. Final Answer:

    #!/bin/bash -> Option A
  4. Quick Check:

    Shebang for Bash = #!/bin/bash [OK]
Hint: Bash scripts start with #!/bin/bash [OK]
Common Mistakes:
  • Using #!/bin/sh which may not be Bash
  • Confusing Python or C shell shebangs
  • Omitting the shebang line
3. What will be the output of this Bash script?
#!/bin/bash
for i in 1 2 3; do
  echo "Task $i done"
done
medium
A. Task 1 done Task 2 done Task 3 done
B. Error: syntax error
C. Task 1 done Task 2 done Task 3 done
D. Task 1 done Task 2 done Task 3 done

Solution

  1. Step 1: Understand the for loop in Bash

    The loop runs three times with i=1, then 2, then 3.
  2. Step 2: Check the echo command output

    Each echo prints "Task $i done" followed by a newline, so output lines appear separately.
  3. Final Answer:

    Task 1 done Task 2 done Task 3 done -> Option A
  4. Quick Check:

    Loop prints each task on new line [OK]
Hint: echo adds a new line after each output [OK]
Common Mistakes:
  • Thinking echo outputs on same line
  • Confusing spaces with newlines
  • Expecting syntax errors in correct loops
4. Find the error in this Bash script that should print numbers 1 to 3:
#!/bin/bash
for i in {1..3}
do
  echo "Number: $i"
fi
medium
A. Incorrect shebang line
B. Wrong variable name used
C. Echo command syntax error
D. Missing 'done' to close the for loop

Solution

  1. Step 1: Check loop syntax

    The for loop must end with done, but script ends with fi which is for if statements.
  2. Step 2: Identify the correct closing keyword

    Replace fi with done to properly close the loop.
  3. Final Answer:

    Missing 'done' to close the for loop -> Option D
  4. Quick Check:

    Loops end with done, not fi [OK]
Hint: For loops end with done, not fi [OK]
Common Mistakes:
  • Using fi instead of done to close loops
  • Confusing if and for syntax
  • Ignoring missing loop closure errors
5. You want to automate cleaning temporary files every day using a Bash script. Which approach best uses Bash scripting to automate this task?
hard
A. Manually run rm commands daily in the terminal
B. Use a graphical file manager to delete files once a week
C. Write a Bash script that deletes temp files and schedule it with cron
D. Write a Python program to delete files but run it manually

Solution

  1. Step 1: Identify automation with Bash scripting

    Writing a Bash script to delete temp files automates the task by running commands automatically.
  2. Step 2: Use cron to schedule the script

    Cron is a Linux tool to run scripts at set times, enabling daily automatic cleanup without manual work.
  3. Final Answer:

    Write a Bash script that deletes temp files and schedule it with cron -> Option C
  4. Quick Check:

    Bash script + cron = automated daily task [OK]
Hint: Combine Bash script with cron for automatic scheduling [OK]
Common Mistakes:
  • Running commands manually defeats automation
  • Using GUI tools is not scripting
  • Writing scripts but not scheduling them
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