All Topics  
Shell script

 

   Email Print
   Bookmark   Link






 

Shell script



 
 
A shell script is a script
Scripting language

A scripting language, script language or extension language, is a programming language that allows some control of a single or many Application software....
 written for the shell
Shell (computing)

In computing, a shell is a piece of software that provides an Interface for users. Typically, the term refers to an operating system shell which provides access to the services of a kernel ....
, or command line interpreter
Command line interpreter

A command-line interpreter is a computer program that reads lines of text entered by a user and interprets them in the context of a given operating system or programming language....
, of an operating system
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
. It is often considered a simple domain-specific programming language
Domain-specific programming language

In software development, a domain-specific language is a programming language or specification language dedicated to a particular problem domain, a particular problem representation technique, and/or a particular solution technique....
. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Many shell script interpreters double as command line interface
Command line interface

A command-line interface is a mechanism for interacting with a computer operating system or software by typing commands to perform specific tasks....
, such as the various Unix shells, Windows PowerShell
Windows PowerShell

Windows PowerShell is an extensible command line interface shell and associated scripting language from Microsoft. It was released in 2006 and is currently available for Windows XP SP2/SP3, Windows Server 2003, Windows Vista and is included in Windows Server 2008 as well as Windows 7 as an optional feature....
 or the MS-DOS
MS-DOS

MS-DOS is an operating system commercialized by Microsoft. It was the most commonly used member of the DOS family of operating systems and was the main operating system for personal computers during the 1980s....
 COMMAND.COM. Others, such as AppleScript
AppleScript

AppleScript is a scripting language devised by Apple Inc., and built into Mac OS. More generally, "AppleScript" is the word used to designate the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface....
 or the graphical Windows Script Host
Windows Script Host

The Microsoft Windows Script Host is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features....
 (WScript.exe), add scripting capability to computing environments without requiring a command line interface.






Discussion
Ask a question about 'Shell script'
Start a new discussion about 'Shell script'
Answer questions from other users
Full Discussion Forum



Encyclopedia


A shell script is a script
Scripting language

A scripting language, script language or extension language, is a programming language that allows some control of a single or many Application software....
 written for the shell
Shell (computing)

In computing, a shell is a piece of software that provides an Interface for users. Typically, the term refers to an operating system shell which provides access to the services of a kernel ....
, or command line interpreter
Command line interpreter

A command-line interpreter is a computer program that reads lines of text entered by a user and interprets them in the context of a given operating system or programming language....
, of an operating system
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
. It is often considered a simple domain-specific programming language
Domain-specific programming language

In software development, a domain-specific language is a programming language or specification language dedicated to a particular problem domain, a particular problem representation technique, and/or a particular solution technique....
. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Many shell script interpreters double as command line interface
Command line interface

A command-line interface is a mechanism for interacting with a computer operating system or software by typing commands to perform specific tasks....
, such as the various Unix shells, Windows PowerShell
Windows PowerShell

Windows PowerShell is an extensible command line interface shell and associated scripting language from Microsoft. It was released in 2006 and is currently available for Windows XP SP2/SP3, Windows Server 2003, Windows Vista and is included in Windows Server 2008 as well as Windows 7 as an optional feature....
 or the MS-DOS
MS-DOS

MS-DOS is an operating system commercialized by Microsoft. It was the most commonly used member of the DOS family of operating systems and was the main operating system for personal computers during the 1980s....
 COMMAND.COM. Others, such as AppleScript
AppleScript

AppleScript is a scripting language devised by Apple Inc., and built into Mac OS. More generally, "AppleScript" is the word used to designate the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface....
 or the graphical Windows Script Host
Windows Script Host

The Microsoft Windows Script Host is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features....
 (WScript.exe), add scripting capability to computing environments without requiring a command line interface. Other examples of programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s primarily intended for shell scripting include DCL
DIGITAL Command Language

DCL, the DIGITAL Command Language, is the standard command languageadopted by most of the operating systems that were sold by the former Digital Equipment Corporation ....
 and JCL
Job Control Language

Job Control Language is a scripting language used on IBM mainframe operating systems to instruct the system on how to run a batch processing or start a subsystem....
.

Capabilities

In their most basic form, shell scripts allow several commands that would be entered "by hand" at a command line interface to be executed automatically and rapidly. For example, the following Bourne shell
Bourne shell

The Bourne shell, or sh, was the default Unix shell of Version 7 Unix, and replaced the Thompson shell, whose executable file had the same name, sh....
 script copies all text files (*.txt) and MP3 files
MP3

MPEG-1 Audio Layer 3, more commonly referred to as MP3, is a digital audio Encoder format using a form of lossy data compression. It is a common audio format for consumer audio storage, as well as a de facto standard encoding for the transfer and playback of music on digital audio players....
 (*.mp3) in the working directory
Working directory

In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process....
 to the root directory
Root directory

In computing file systems, the root directory is the first or top-most directory in a hierarchy. It can be likened to the root of a tree - the starting point where all branches originate....
:

cp *.txt / cp *.mp3 /

This example also demonstrates the use of wildcard character
Wildcard character

The term wildcard character has the following meanings:...
s, a simple way of matching multiple related files ("*" denotes any sequence of characters). Most shells implement basic pattern matching
Pattern matching

In computer science, pattern matching is the act of checking for the presence of the constituents of a given pattern. In contrast to pattern recognition, the pattern is rigidly specified....
 capabilities like this, which allow them to perform commands on groups of items with similar names and sometimes parse simple strings of text.

Although each shell scripting language is different, there are a number of additional features which are often provided. One is a mechanism for manipulating some form of variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
s, in other words, named values which can be inserted elsewhere in the script at specified locations. For example, this script copies all .txt and .mp3 files from the current directory to the directory named by the variable "fuzzy":

cp *.txt $fuzzy cp *.mp3 $fuzzy

By changing the value of the variable "fuzzy", the user can specify to where the files are copied. However, rewriting a script to change a variable's definition each time the script is run would be a nuisance, so scripting languages typically also support special variables which provide access to any arguments passed to the script on the command line. For example, $1 through $9 refer to the first nine arguments given to a Bourne shell script, as do %1 through %9 in DOS batch files. Also, a shell's internal variables are often integrated with the operating system's notion of per-process or per-session environment variable
Environment variable

Environment variables are a set of dynamic named Value s that can affect the way running computer process will behave on a computer....
s.

Another common feature of shell scripting languages is some way of dealing with return codes, which are numbers returned from executed programs to indicate whether they succeeded or failed. In the Bourne shell, for example, the notation a && b means to first execute a, then execute b only if a succeeded.

Many modern shells also supply various features usually found only in more sophisticated general-purpose programming languages, such as control-flow constructs (if, while, goto), mutable variables, comments
Comment (computer programming)

In computer programming, a comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program....
, subroutine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
s, and so on. With these sorts of features available, it is sometimes possible to write reasonably sophisticated applications as shell scripts, although of course the more demanding, complex or large-scale systems will usually require more powerful programming languages. Though the shells are powerful in their way, they have few structuring mechanisms, limited built-in commands, and are generally interpreted relatively slowly.

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 language
High-level programming language

In computing, a high-level programming language is a programming language with strong Abstraction from the details of the computer. In comparison to low-level programming languages, it may use natural language elements, be easier to use, or more Porting across platforms....
s is a frequent source of debate.

Advantages and disadvantages

Often, writing a shell script is much quicker than writing the equivalent code in other programming languages. The many advantages include easy program or file selection, quick start, and interactive debugging. A shell script can be used to provide a sequencing and decision-making linkage around existing programs, and for moderately-sized scripts the absence of a compilation step is an advantage. Interpretive running makes it easy to write debugging code into a script and rerun it to detect and fix bugs. Non-expert users can use scripting to tailor the behavior of programs, and shell scripting provides some limited scope for multiprocessing.

On the other hand, shell scripting is prone to costly errors. Inadvertent typing errors such as rm
Rm (Unix)

rm is a Unix command used to delete files from a filesystem....
 -rf * /
(instead of the intended rm -rf */) are folklore in the Unix community; a single extra space converts the command from one that deletes everything in the sub-directories to one which deletes everything - and also tries to delete everything on the root partition. Similar problems can transform cp
Cp (Unix)

cp is the command entered in a Unix shell to copy a computer file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name....
and mv into dangerous weapons, and misuse of the > redirect can delete the contents of a file. This is made more problematic by the fact that many UNIX commands differ in name by only one letter: cp, cn, cd.

Another significant disadvantage is the slow execution speed and the need to launch a new process for almost every shell command executed. When a script's job can be accomplished by setting up a pipeline in which efficient filter
Filter (software)

A filter is a computer program to process a data stream. Some operating systems such as Filter are rich with filter programs. Even Microsoft Windows has some simple filters built in to its command shell, most of which have significant enhancements relative to the similar filter commands that were available in MS-DOS....
 commands perform most of the work, the slowdown is mitigated, but a complex script is typically several orders of magnitude slower than a conventional compiled program that performs an equivalent task.

There are also compatibility problems between different platforms. Larry Wall
Larry Wall

Larry Wall is a programmer and author, most widely known for his creation of the Perl programming language in 1987....
, creator of Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, famously wrote that "It is easier to port a shell than a shell script."

Similarly, more complex scripts can run into the limitations of the shell scripting language itself; the limits make it difficult to write quality code and extensions by various shells to ameliorate problems with the original shell language can make problems worse.

Many disadvantages of using some script languages are caused by design flaws within the language syntax or implementation, and are not necessarily imposed by the use of a text-based command line; there are a number of shells which use other shell programming languages or even full-fledged languages like Scsh
Scsh

Scsh is a POSIX API layered on top of the Scheme in a manner to make the most of Scheme's capability for scripting. It is limited to 32-bit platforms....
 (which uses Scheme).

See also

  • Glue code
    Glue code

    In programming, glue code is code that does not contribute any functionality towards meeting the program's Requirements, but instead serves solely to "glue together" different parts of code that would not otherwise be compatible....
  • Shebang symbol (#!)
    Shebang (Unix)

    In computing, a shebang refers to the characters "#!" when they are the first two characters in a script file. Unix-like operating systems take the presence of these two characters as an indication that the file is a script, and try to execute that script using the interpreter specified by the rest of the first line in the file....
  • Unix shell
    Unix shell

    A Unix shell is a command-line interpreter and script host that provides a traditional user interface for the Unix operating system and for Unix-like systems....
    s
  • Windows PowerShell
    Windows PowerShell

    Windows PowerShell is an extensible command line interface shell and associated scripting language from Microsoft. It was released in 2006 and is currently available for Windows XP SP2/SP3, Windows Server 2003, Windows Vista and is included in Windows Server 2008 as well as Windows 7 as an optional feature....
  • Windows Script Host
    Windows Script Host

    The Microsoft Windows Script Host is an automation technology for Microsoft Windows operating systems that provides scripting capabilities comparable to batch files, but with a greater range of supported features....


External links