Making a Script Executable

In the previous section, we showed you that you can run a shell script by typing sh followed by the name of the shell script file. You can also make a script executable, which means that you can run it simply by typing the script name at the shell prompt (omitting the name of the shell). Doing so is handy because it allows you to use the script as conveniently as you’d use any other command. As Code Listing 10.2 shows, you must set up a little before you can just execute the script.

Code Listing 10.2. After a little one-time preparation, you can run executable scripts by typing the script name at the shell prompt.
[ejr@hobbes scripting]$ head -2 myscript #!/bin/sh # This is my first shell script [ejr@hobbes scripting]$ ...

Get Unix Third Edition: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.