0
0
Bash Scriptingscripting~20 mins

Shebang line (#!/bin/bash) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shebang 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 script with a shebang?
Consider this bash script saved as test.sh and made executable:
#!/bin/bash
echo "Hello from script!"

What will be the output when you run ./test.sh in a terminal?
Bash Scripting
#!/bin/bash
echo "Hello from script!"
AHello from script!
Bbash: ./test.sh: command not found
CSyntax error near unexpected token `echo'
DNo output, script runs silently
Attempts:
2 left
💡 Hint
The shebang line tells the system which program to use to run the script.
💻 Command Output
intermediate
2:00remaining
What happens if the shebang line is missing?
Consider this script saved as myscript.sh and made executable with chmod +x myscript.sh:
echo "Running script"

What will be the output when you run ./myscript.sh in a terminal?
Bash Scripting
echo "Running script"
ANothing happens, script does not run
Bbash: ./myscript.sh: cannot execute binary file: Exec format error
CSyntax error: missing shebang
DRunning script
Attempts:
2 left
💡 Hint
Without a shebang, the system may not know how to run the script.
📝 Syntax
advanced
2:00remaining
Identify the correct shebang line for bash scripts
Which of the following lines is the correct shebang to start a bash script?
A#!/bin/bash -c
B#!/bin/bash
C#!/bin/sh -x
D#!/usr/bin/python
Attempts:
2 left
💡 Hint
The shebang must point to the bash executable without extra arguments.
🔧 Debug
advanced
2:00remaining
Why does this script fail to run with ./script.sh?
You have this script:
#!/bin/bash
echo "Start"
exit 0

But when you run ./script.sh, you get bash: ./script.sh: Permission denied. What is the cause?
Bash Scripting
#!/bin/bash
echo "Start"
exit 0
AThe echo command is misspelled
BThe shebang line is incorrect
CThe script file does not have execute permission
DThe script has a syntax error
Attempts:
2 left
💡 Hint
Check the file permissions with ls -l.
🚀 Application
expert
2:00remaining
How to write a portable shebang line for bash scripts?
You want your bash script to run on different systems where bash might be in different locations. Which shebang line is best to ensure portability?
A#!/usr/bin/env bash
B#!/bin/bash
C#!/bin/sh
D#!/usr/local/bin/bash
Attempts:
2 left
💡 Hint
Use env to find bash in the user's PATH.