← New search

Other meanings of Shell script

COMPUTING · COMMAND-LINE PROGRAMMING

Shell script

A shell script is a text file containing commands interpreted by a command-line shell such as the Unix shell, Bash, or another POSIX-compatible shell. It combines external programs, shell syntax, variables, control flow, and input/output redirection to automate tasks that would otherwise be entered interactively.

1970s
Origins
Unix command interpreters and scriptable utilities
sh
Portability baseline
The traditional POSIX shell language
#!/bin/sh
Common launcher
Shebang selecting a shell interpreter
1

Definition and execution

A shell script is a sequence of commands read and interpreted by a command-line shell. The shell can invoke programs, connect them with pipelines, redirect files, expand variables, and make decisions with conditionals and loops. Unix shells grew from the command interpreters developed for Unix, where the same interface served both interactive use and automation.1

A script may be run explicitly, such as with bash backup.sh, or made directly executable by placing an interpreter directive, called a shebang, on its first line and setting the file's execute permission. The operating system then starts the named interpreter; the script itself is normally not compiled into machine code. Exit status is central: a command returns a numerical status, conventionally zero for success, which later commands or calling programs can test.

2

Language features and portability

Shell scripting combines shell grammar with the capabilities of ordinary command-line utilities. POSIX specifies core language features including quoting, parameter expansion, command substitution, pipelines, redirections, functions, and control structures, while individual shells add extensions.2

Portability depends on distinguishing the standardized language from features specific to Bash, Z shell, KornShell, or other interpreters. A script beginning with #!/bin/sh should use the portable sh language rather than Bash-only syntax such as arrays or [[ ... ]]; a Bash script should instead identify Bash explicitly. Quoting is a major source of correctness problems because unquoted expansions can undergo word splitting and pathname expansion. Robust scripts also check command results, use clear variable names, and avoid parsing human-oriented output when a structured interface is available.

3

Uses, strengths, and security

Shell scripts are strongest at orchestrating existing programs and operating-system facilities rather than performing computation-heavy work. They are widely used for installation, backups, system administration, build steps, testing, data movement, log processing, and scheduled jobs. Pipes and redirections make small tools composable, while environment variables and positional parameters allow the same script to serve different contexts.

Their convenience also creates security risks. Data supplied by users, files, or network services must not be inserted into commands without careful quoting and validation; unsafe temporary files, unexpected filenames, altered PATH values, and excessive privileges can turn automation into code execution. The 2014 Shellshock vulnerabilities demonstrated that flaws in shell environment-function handling could affect network-facing services that invoked Bash.3 ShellCheck, a static-analysis tool, detects many common quoting, expansion, and portability mistakes before deployment.4

4

Lesser-known aspects

Shell scripts are not limited to Bash, and “shell script” is therefore a family description rather than a single language name. The Almquist shell, dash, ksh, mksh, zsh, fish, and several embedded or specialized interpreters differ in syntax, startup behavior, and compatibility. On many Linux systems, /bin/sh points to a smaller shell such as dash, whose emphasis on speed and POSIX compatibility makes it common for system scripts.

Less visible mechanisms also matter. A script can receive standard input, standard output, and standard error as independent streams; can run jobs asynchronously; and can be affected by shell options, locale settings, signals, and inherited environment variables. Command substitution removes trailing newline characters, while glob patterns may expand differently when no file matches. These edge cases explain why mature scripts often set deliberate error-handling and globbing policies, use temporary directories safely, and document the interpreter and assumptions on which their behavior depends.25

Glossary

Shebang
The first-line interpreter directive, commonly written as <code>#!/bin/sh</code> or <code>#!/usr/bin/env bash</code>.
Pipeline
A command structure that sends one program's standard output to another program's standard input.
Exit status
The integer result returned by a command or script, conventionally zero for success.
POSIX shell
The standardized shell language and utility interface specified by the IEEE and The Open Group.
Command substitution
A shell expansion that replaces a command expression with the command's captured standard output.

Shell scripts are interpreter-specific unless they deliberately restrict themselves to a documented portable shell language.