
A shell script is a
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components.
A computer progra ...
designed to be run by a
Unix shell
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system t ...
, a
command-line interpreter
A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive command (computing), commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invokin ...
. The various dialects of shell scripts are considered to be
scripting language
A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.
A scripti ...
s. 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 (MSDos-Win95 stream,
OS/2
OS/2 (Operating System/2) is a series of computer operating systems, initially created by Microsoft and IBM under the leadership of IBM software designer Ed Iacobucci. As a result of a feud between the two companies over how to position OS/2 ...
), command procedures (VMS), and shell scripts (
Windows NT
Windows NT is a proprietary graphical operating system produced by Microsoft, the first version of which was released on July 27, 1993. It is a processor-independent, multiprocessing and multi-user operating system.
The first version of Wi ...
stream and third-party derivatives like
4NT—article is at
cmd.exe), and mainframe operating systems are associated with a number of terms.
Shells commonly present in Unix and Unix-like systems include the
Korn shell
KornShell (ksh) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on Bourne shell source code. Other early contributors were Bell L ...
, the
Bourne shell
The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.
The Bourne shell was the default shell for Version 7 Unix. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic link ...
, and
GNU Bash
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was ...
. While a Unix operating system may have a different default shell, such as
Zsh
The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh.
Hist ...
on
macOS
macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac (computer), Mac computers. Within the market of ...
, these shells are typically present for backwards compatibility.
Capabilities
Comments
Comments
Comment may refer to:
* Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence
* Bernard Comment (born 1960), Swiss writer and publisher
Computing
* Comment (computer programming), explanatory text or informa ...
are ignored by the shell. They typically begin with the hash symbol (
#
), and continue until the end of the line.
[Johnson, Chris (2009)]
''Pro Bash Programming: Scripting the Linux Shell'', Apress, Retrieved on September 27, 2019.
Configurable choice of scripting language
The
Shebang (Unix), 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
LC_COLLATE=C ls -FCas "$@"
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 (none omitted), and a size in blocks. The
LC_COLLATE=C
sets the default collation order to not fold upper and lower case together, not intermix
dotfile
In computing, a hidden folder (sometimes hidden directory) or hidden file is a folder or file which filesystem utilities do not display by default when showing a directory listing. They are commonly used for storing user preferences or preservin ...
s with normal filenames as a side effect of ignoring punctuation in the names (dotfiles are usually only shown if an option like
-a
is used), 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
clear
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.
Note: If an implementation does not have the
clear command, try using the
clr command instead.
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
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming inte ...
-compliant shells, here named
build
and kept in the directory with them, which would compile them automatically:
#!/bin/sh
printf 'compiling...\n'
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
for jpg; do # use $jpg in place of each filename given, in turn
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 (provided by ImageMagick) 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 *.jpg
Programming
Many modern shells also supply various features usually found only in more sophisticated
general-purpose programming language
In computer software, a general-purpose programming language (GPL) is a programming language for building software in a wide variety of application domains. Conversely, a domain-specific programming language is used within a specific area. For ex ...
s, such as control-flow constructs, variables,
comments
Comment may refer to:
* Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence
* Bernard Comment (born 1960), Swiss writer and publisher
Computing
* Comment (computer programming), explanatory text or informa ...
, arrays,
subroutine
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions ma ...
s 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
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
can also be embedded in shell scripts as can other scripting languages like
Tcl. Perl and Tcl come with graphics toolkits as well.
Typical POSIX scripting languages
Scripting languages commonly found on UNIX, Linux, and POSIX-compliant operating system installations include:
*
KornShell
KornShell (ksh) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on Bourne shell source code. Other early contributors were Bell ...
(
ksh
) in several possible versions such as ksh88, Korn Shell '93 and others.
* The
Bourne shell
The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.
The Bourne shell was the default shell for Version 7 Unix. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic link ...
(
sh
), one of the oldest shells still common in use
* The
C shell
The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the B ...
(
csh
)
*
GNU Bash
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was ...
(
bash
)
*
tclsh
Tk is a free and open-source, cross-platform widget toolkit that provides a library of basic elements of GUI widgets for building a graphical user interface (GUI) in many programming languages.
Tk provides a number of widgets commonly needed to ...
, a shell which is a main component of the
Tcl/Tk
Tk is a free and open-source, cross-platform widget toolkit that provides a library of basic elements of GUI widgets for building a graphical user interface (GUI) in many programming languages.
Tk provides a number of widgets commonly needed to ...
programming language.
* The
wish
A wish is a hope or desire for something. In fiction, wishes can be used as plot devices. In folk