From typed commands to a file
Everything you type at the prompt can go into a file. The file is a script, and it is nothing more than plain text. Bash reads the file one line at a time, starting at the top, and runs each command the same way it would run a command typed by hand. Blank lines are ignored. A hash sign starts a comment that runs to the end of the line, so you can describe what the script does for the next reader. Comments cost nothing at run time, and they are the cheapest documentation a script can have.
Scripts are often saved with a .sh extension. The extension is a convention for people and editors, not a rule for the system. Linux does not care about the name, so a script called deploy with no extension works just as well. What matters is the content of the first line and the permission bits on the file.
cat > hello.sh << 'EOF'
#!/bin/bash
# Print one sentence.
echo "hello from a script"
EOFThe shebang line
The first unit of this module has one more practical point. When you write a script, you choose the interpreter, and that choice is part of the script behavior. A script that uses bash-only features such as double brackets must name bash in its shebang. A script that is meant to run under any POSIX shell should avoid those features, and should name sh. Writing the shebang deliberately makes that promise visible to the next reader.
The first two characters of a script can be #!. Together they are called the shebang, or hash-bang. When the kernel is asked to run a file, it looks at those two bytes. If they are present, the rest of the first line is treated as the path to an interpreter, and the kernel starts that interpreter with the script file as its argument. The line #!/bin/bash therefore means that bash should run the file.
Bash itself treats the shebang as an ordinary comment, because it begins with a hash sign. The line matters only to whoever starts the file. For the same reason, you can write a shebang for any language, such as #!/usr/bin/env python3, and the kernel will hand the file to Python.
The path in the shebang is absolute, so /bin/bash has to exist at that location, and most Linux systems put bash there. The form #!/usr/bin/env bash asks env to search PATH for bash instead. That form is more portable, because bash can live in a different place on some systems, such as a Homebrew install on macOS. The lab for this course puts bash at /bin/bash, so either form works there.
Two details trip people up. The first is line endings. A file edited on Windows may end each line with a carriage return followed by a line feed. The shebang then names /bin/bash followed by a carriage return, and the error says no such file or directory, even though bash exists. The fix is to save the file with Unix line endings. The second is length. The kernel reads only the first part of the file to find the interpreter, so keep the shebang short.
Two ways to run a script
Bash can run a script in two ways, and they behave differently. The first is to pass the file to bash as an argument, as in bash hello.sh. Here you name the interpreter yourself, so the shebang is ignored, and the file does not need the execute permission. Bash only needs to read the file. This form is useful for a quick test, and it works even on a file with no permission bits set.
The second way is to run the file by its path. The file must carry the execute permission for you, and the kernel then reads the shebang and starts the interpreter. You make a file executable with chmod. The usual pair of commands looks like this:
chmod +x hello.sh
./hello.shThe mode column of ls -l shows the permissions. A runnable file shows an x in the owner position, and after chmod you should see something like -rwxr-xr-x. The execute bit is not about the contents. A file without the bit set cannot be started by its path, however good its contents are, and a file with the bit set is still just text that the kernel hands to an interpreter.
If you forget chmod, the path form fails with a clear message. Bash prints a permission error and returns exit status 126, which means the command was found but could not be run. Status 126 and its neighbors are covered in the exit codes unit of this module.
The two forms can give different results for the same file. A file with the wrong shebang can still run with bash file, because the shebang is never read. Running it by path would then fail. When a script works one way and not the other, compare the shebang with the interpreter you expect.
Why the ./ prefix matters
Bash looks up a command name without a slash by searching the directories listed in PATH, and the current directory is usually not one of them. So typing hello.sh alone fails, even when the file is executable. The message is command not found, and the exit status is 127. The prefix ./ tells bash that the name is a path relative to the current directory. You can also type a full path, such as /workspace/hello.sh, which has the same effect.
Adding the current directory to PATH is possible, but it is a security risk. Anyone who can write to a directory in your PATH can place a program there that shadows a real command. Typing ./ is a small habit that avoids the risk and also makes it obvious which file runs.
Where the script runs
When you run a script, the kernel starts a new process for it. That process is a separate bash, and it has its own variables, its own working directory, and its own exit status. Variables set in the script do not appear in your shell afterward, and a cd inside the script does not move you. The script exit status becomes the status of the command you ran, which is what $? reports.
That isolation is useful. A script can change its variables freely without damaging the environment of the shell that started it. When you do want the variables to remain, you use the source command, which runs a file inside the current shell. A later module covers sourcing files in detail.
Hands-on: write and run one script
Create a script in /workspace, which is your home directory in the lab. The block below writes the file with a here-document, a way to put several lines into one command without opening an editor.
cat > hello.sh << 'EOF'
#!/bin/bash
echo "hello from a script"
EOFRun it first with bash hello.sh. Then make it executable with chmod +x hello.sh and run it with ./hello.sh. Both runs print the same sentence. Next, remove the execute bit with chmod -x hello.sh and try ./hello.sh again. You should see the permission error. Run echo $? on the next line to confirm the status 126.
Most scripts need more than a fixed sentence. When a script receives values from the command line, it reads them as positional parameters, and the next unit covers those in detail.