A systems administrator writes a shell script, deploy.sh, which contains syntax specific to the Bash shell. When the administrator executes it with the command bash deploy.sh, it runs successfully. However, when a colleague runs the script directly via ./deploy.sh, it immediately fails with syntax errors. The administrator has already confirmed that the script has execute permissions set correctly. What is the most likely cause for the script failing only when run directly?
The colleague does not have their default login shell set to Bash.
The script was edited on a Windows machine and has incorrect line endings (CRLF).
The script is missing a #!/bin/bash interpreter directive on the first line.
The colleague's PATH environment variable is not configured correctly.
The correct answer is that the script is missing the #!/bin/bash interpreter directive. The #! sequence, known as a shebang, is a special directive on the first line of a script that tells the system which program should be used to interpret the script's contents. When a script is run directly (e.g., ./deploy.sh), the system's program loader looks for this shebang line. If the line is #!/bin/bash, the script is passed to the Bash interpreter. If the shebang is missing, the script is executed using the user's current or default shell. In this scenario, the colleague's default shell is likely not Bash (e.g., it could be sh), which does not understand Bash-specific syntax, leading to syntax errors. Explicitly running the script with bash deploy.sh bypasses the shebang mechanism and forces the use of the Bash interpreter, which is why it works for the administrator.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is a shebang (`#!`) and how does it work in a script?
Open an interactive chat with Bash
Why does the absence of a shebang cause syntax errors when run directly?
Open an interactive chat with Bash
How can you verify which shell is being used to execute a script?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access