Shell script

related topics
{math, number, function}
{system, computer, user}
{language, word, form}

A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Many shell script interpreters double as command line interface, such as the various Unix shells, Windows PowerShell or the MS-DOS COMMAND.COM. Others, such as AppleScript or the graphical Windows Script Host (WScript.exe), add scripting capability to computing environments without requiring a command line interface. Other examples of programming languages primarily intended for shell scripting include DCL and JCL.




In their most basic form, a shell script can provide a convenient variation of a system command where special environment settings, command options, or postprocessing is applied automatically, but in a way that allows the new script to still act as a fully normal Unix command.

One example would be to create a version of ls, the command to list files, giving it a shorter command name of l, which would be normally saved in a user's bin directory as /home/username/bin/l, and a default set of command options pre-supplied.

LC_COLLATE=C ls -FCas "$@"

Here, the first line (Shebang) indicates what interpreter should be used to execute the rest of the script, and the second line makes a listing with options for file format indicators, columns, all files (none omitted), and a size in blocks. The LC_COLLATE=C set the default collation order to not fold upper and lower case together, and the "$@" causes any parameters given to l to be passed through as parameters to ls, so that all of the normal options and other syntax known to ls can still be used.

The user would then be able to simply use l for the most commonly used short listing.

[edit] Batch jobs

Shell scripts allow several commands that would be entered manually at a command line interface to be executed automatically, and without having to wait for a user to trigger each stage of the sequence. For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a C shell script, here named build and kept in the directory with them, which would compile them automatically:

echo compiling...
cc -c foo.c
cc -c bar.c
cc -c qux.c
cc -o myprog foo.o bar.o qux.o 
echo done.

The script would allow a user to save the file being edited, pause the editor, and then just run ./build to create the updated program, test it, and then return to the editor. Since the 1980s or so, however, scripts of this type have been replaced with utilities like make which are specialized for building programs.

[edit] Generalization

Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users. A Bourne-Again shell script to convert JPEG images to PNG images, where the image names are provided on the command line - possibly via wildcards - instead of each being listed within the script, can be created with this file, typically saved in a file like /home/username/bin/jpg2png

for jpg in "$@" ; do                         # use $jpg in place of each filename given, in turn
    png="${jpg%.jpg}.png"                    # find the PNG version of the filename by replacing .jpg with .png
    echo converting "$jpg" ...               # output status info to the user running the script
    if convert "$jpg" ; then      # use the convert program (common in Linux) to create the PNG in a temp file
        mv "$png"                 # if it worked, rename the temporary PNG image to the correct name
    else                                     # ...otherwise complain and exit from the script
        echo 'error: failed output saved in "".' 1>&2
        exit 1
    fi                                       # the end of the "if" test construct
done                                         # the end of the "for" loop
echo all conversions successful              # tell the user the good news

The jpg2png command can then be run on an entire directory full of JPEG images with just jpg2png *.jpg

[edit] Verisimilitude

A key feature of shell scripts is that the invocation of their interpreters are handled as a core operating system feature. So rather than a user's shell only being able to execute scripts in that shell's language, or a script only having its interpreter directive handled correctly if it was run from a shell (both of which were limitations in the early Bourne shell's handling of scripts), shell scripts are set up and executed by the OS itself. A modern shell script is not just on the same footing as system commands, but rather many system commands are actually shell scripts (or more generally, scripts, since some of them are not interpreted by a shell, but instead by Perl, Python, or some other language). This extends to returning exit codes like other system utilities to indicate success or failure, and allows them to be called as components of larger programs regardless of how those larger tools are implemented.

Like standard system commands, shell scripts classically omit any kind of filename extension unless intended to be read into a running shell through a special mechanism for this purpose (such as sh's ".", or csh's source).

[edit] Programming

Many modern shells also supply various features usually found only in more sophisticated general-purpose programming languages, such as control-flow constructs, variables, comments, arrays, subroutines, and so on. With these sorts of features available, it is possible to write reasonably sophisticated applications as shell scripts. However, they are still limited by the fact that most shell languages have little or no support for data typing systems, classes, threading, complex math, and other common full language features, and are also generally much slower than compiled code or interpreted languages written with speed as a performance goal.

[edit] Other scripting languages

Many powerful scripting languages have been introduced for tasks that are too large or complex to be comfortably handled with ordinary shell scripts, but for which the advantages of a script are desirable and the development overhead of a full-blown, compiled programming language would be disadvantageous. The specifics of what separates scripting languages from high-level programming languages is a frequent source of debate. But generally speaking a scripting language is one which requires an interpreter.

[edit] Life cycle

Shell scripts often serve as an initial stage in software development, and are often subject to conversion later to a different underlying implementation, most commonly being converted to Perl, Python, or C. The interpreter directive allows the implementation detail to be fully hidden inside the script, rather than being exposed as a filename extension, and provides for seamless reimplementation in different languages with no impact on end users.

Full article ▸

related documents
Logo (programming language)
Bilinear transform
Infinite loop
Latin square
Information retrieval
Generating trigonometric tables
Interpolation search
Integral domain
Compactness theorem
Bolzano–Weierstrass theorem
Pauli matrices
Procedural programming
Compact space
Union (set theory)
Elliptic integral
Chain complex
Perfect number
Constructible number
Free variables and bound variables
Linear search
Jacobi symbol
Closure (topology)