Shell scripts are text files; subsequently one must use a text editor to produce them (not a word processor!) and then make the file executable so that it can be run. All Bash scripts contain a first line in one of two forms…
#!/bin/bash
or
#!/bin/sh
The #! tells the shell that the file contains executable commands and the path that follows tells the shell which program to run the commands with. The program used to run the script can be PERL, SED, AWK, another language or another shell.
The first option indicates that the shell is to use the Bash program itself (/bin/bash) to execute the script. The second option indicates the script is to be executed by (/bin/sh) which is a symbolic link to bash for backwards compatibility with older scripts. sh is the name of the old Bourne shell which Bash has replaced. Bash can run any Bourne script and many people still use /bin/sh to begin their scripts.
Example…
Write a Bash script that outputs the text “Hello world” to stdout…
Step 1. Create a new text document using a text editor and add the following lines…
#!/bin/bash
echo Hello world
then save the file as hello.
Step 2. Using chmod change the permissions on the file to make it executable…
$ chmod a+x hello <Enter>