Shell script
A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be command languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.
The term is also used more generally to mean the automated mode of running an operating system shell; each operating system uses a particular name for these functions including batch files, command procedures, and shell scripts, and mainframe operating systems are associated with a number of terms.
All Unix-like systems include at least one POSIX shell.
Capabilities
Comments
are ignored by the shell. They typically begin with the hash symbol, and continue until the end of the line.Configurable choice of scripting language
The shebang, or hash-bang, is a special kind of comment which the system uses to determine what interpreter to use to execute the file. The shebang must be the first line of the file, and start with "#!". In Unix-like operating systems, the characters following the "#!" prefix are interpreted as a path to an executable program that will interpret the script.Shortcuts
A shell script can provide a convenient variation of a system command where special environment settings, command options, or post-processing apply 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.- !/bin/sh
Here, the first line uses a shebang to indicate which interpreter should execute the rest of the script, and the second line makes a listing with options for file format indicators, columns, all files, and a size in blocks. The
LC_COLLATE=C sets the default collation order to not fold upper and lower case together, not intermix dotfiles with normal filenames as a side effect of ignoring punctuation in the names, and the "$@" causes any parameters given to l to pass through as parameters to ls, so that all of the normal options and other syntax known to ls can still be used.The user could then simply use
l for the most commonly used short listing.Another example of a shell script that could be used as a shortcut would be to print a list of all the files and directories within a given directory.
- !/bin/sh
ls -al
In this case, the shell script would start with its normal starting line of #!/bin/sh. Following this, the script executes the command clear which clears the terminal of all text before going to the next line. The following line provides the main function of the script. The ls -al command lists the files and directories that are in the directory from which the script is being run. The ls command attributes could be changed to reflect the needs of the user.
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 script for POSIX-compliant shells, here namedbuild and kept in the directory with them, which would compile them automatically:- !/bin/sh
cc -c foo.c
cc -c bar.c
cc -c qux.c
cc -o myprog foo.o bar.o qux.o
printf 'done.\n'
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.Generalization
Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users. A POSIX sh 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- !/bin/sh
png=$.png # construct the PNG version of the filename by replacing.jpg with.png
printf 'converting "%s"...\n' "$jpg" # output status info to the user running the script
if convert "$jpg" jpg.to.png; then # use convert to create the PNG in a temp file
mv jpg.to.png "$png" # if it worked, rename the temporary PNG image to the correct name
else #...otherwise complain and exit from the script
printf >&2 'jpg2png: error: failed output saved in "jpg.to.png".\n'
exit 1
fi # the end of the "if" test construct
done # the end of the "for" loop
printf 'all conversions successful\n' # tell the user the good news
The
jpg2png command can then be run on an entire directory full of JPEG images with just /home/username/bin/jpg2png *.jpgProgramming
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.The standard Unix tools sed and awk provide extra capabilities for shell programming; Perl can also be embedded in shell scripts as can other scripting languages like Tcl. Perl and Tcl come with graphics toolkits as well.
Typical shell languages
Scripting languages commonly found on UNIX, Linux, and POSIX-compliant operating system installations include:- The original Bourne shell, though no longer in common use
- GNU Bash
- Debian Almquist shell
- Z shell if run in compatibility mode
- Nushell
- Fish shell
- xonsh shell, pronounced "conch", a Python-based shell
- PowerShell
Formerly-popular shells include:
- Old shell, a "port of the standard command interpreter from Sixth Edition UNIX"
- KornShell, since mostly replaced by its successor the Z shell
- Tenex C Shell and its predecessor the C shell
So called remote shells such as
are really just tools to run a more complex shell on a remote system and have no 'shell' like characteristics themselves.
Other scripting languages
Many powerful scripting languages, such as Python, Perl, and Tcl, have been introduced to address tasks that are too large, complex, or repetitive to be comfortably handled by traditional shell scripts, while avoiding the overhead associated with compiled languages like C or Java.Although the distinction between scripting languages and general-purpose high-level programming languages is often debated, scripting languages are typically characterized by their interpreted nature, simplified syntax, and primary use in automating tasks, coordinating system operations, and writing "glue code" between components. Even when scripting languages such as Python or JavaScript support compilation to bytecode or use JIT to improve performance, they are still commonly referred to as "scripting languages" due to their historical association with automation, lightweight tooling, and scripting environments rather than standalone application development.
Importantly, scripting is a form of programming. While "scripting" may emphasize lightweight, task-oriented automation, the broader term "programming" encompasses both scripting and software development in compiled or structured languages. As such, scripting involves writing code to instruct a computer to perform specific tasks—meeting the fundamental definition of programming.
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.While files with the ".sh" file extension are usually a shell script of some kind, most shell scripts do not have any filename extension.