0
0
Bash Scriptingscripting~20 mins

Creating a script file (.sh) in Bash Scripting - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shell Script Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:00remaining
What is the output of this script?
Consider this bash script saved as hello.sh and executed with bash hello.sh:
#!/bin/bash
echo "Hello, World!"
What will be printed on the terminal?
Bash Scripting
#!/bin/bash
echo "Hello, World!"
AError: command not found
Bhello world
CHello, World!
DNo output
Attempts:
2 left
💡 Hint
Look at the echo command and what it prints exactly.
📝 Syntax
intermediate
1:30remaining
Which script has a syntax error?
Identify which of these bash scripts will cause a syntax error when run.
A
#!/bin/bash
while true; do echo "Loop"; break; done
B
#!/bin/bash
echo "Start"
if [ $1 -eq 1 ]; then echo "One" fi
C
#!/bin/bash
for i in 1 2 3; do echo $i; done
D
#!/bin/bash
echo "Done"
Attempts:
2 left
💡 Hint
Check if all if statements have proper closing keywords.
🚀 Application
advanced
2:00remaining
What does this script do?
Analyze this bash script and choose the correct description of its behavior:
#!/bin/bash
count=0
while [ $count -lt 3 ]; do
  echo "Count is $count"
  ((count++))
done
echo "Finished counting"
Bash Scripting
#!/bin/bash
count=0
while [ $count -lt 3 ]; do
  echo "Count is $count"
  ((count++))
done
echo "Finished counting"
APrints 'Count is 0' three times then 'Finished counting'
BInfinite loop printing 'Count is 0'
CPrints 'Finished counting' only
DPrints 'Count is 0', 'Count is 1', 'Count is 2' then 'Finished counting'
Attempts:
2 left
💡 Hint
Look at the loop condition and how count changes.
🔧 Debug
advanced
1:30remaining
Why does this script fail to run?
This script is saved as myscript.sh and run with ./myscript.sh. It shows Permission denied. Why?
#!/bin/bash
echo "Running script"
AThe script file does not have execute permission.
BThe shebang line is incorrect.
CThe echo command is misspelled.
DThe script is missing a closing quote.
Attempts:
2 left
💡 Hint
Check file permissions with ls -l.
🧠 Conceptual
expert
2:30remaining
What is the correct way to create a script file that runs on any bash shell?
You want to create a script file named runme.sh that works on any system with bash installed. Which option correctly creates and prepares this script?
ACreate file with content starting with '#!/bin/bash', save it, then run 'chmod +x runme.sh' before executing.
BCreate file with content starting with '#!/bin/sh', save it, then run 'chmod 644 runme.sh' before executing.
CCreate file with content starting with '#!/bin/bash', save it, then run 'chmod 777 runme.sh' before executing.
DCreate file with content starting with '#!/bin/bash', save it, then run 'chmod 600 runme.sh' before executing.
Attempts:
2 left
💡 Hint
The script needs execute permission and the correct shebang for bash.