Introduction to Unix

../_images/ksus5.jpg ../_images/UNIX_blocks5.jpeg

4.14. A Shell Script ProgramΒΆ

One of the interesting things about Unix is that any commands that may be entered on the command line, may also be saved in a file and run as a shell script program. Sometimes, users develop simple one or two line shell scripts just a convenience to easily run other commands with specific options that may be difficult to remember. Shell scripts can also be more complex using programming constructs such as selection statements and loops. (Covered in Chapter 6)

After the commands have been entered into a file, the program may be executed in one of a couple ways.

  1. A new shell may be started to run the shell script.

    $ bash myscript
    
  2. The shell script file might be given the execute bit and run as a stand alone script.

    $ chmod +x myscript
    $ ./myscript
    

    Note: ./ was used to specify the path of the script. For security reasons, the current directory, ., is often not in the PATH environment variable. If this is the case, then ./ is used to create a fully qualified relative path name so the shell can find the program.

  3. The shell script may also be run using the current shell (see source)

    $ source myscript