Content deleted Content added
Tags: Incorrectly formatted external link or image repeating characters nonsense characters |
c/e hatnote |
||
(296 intermediate revisions by more than 100 users not shown) | |||
Line 1:
{{short description|Script written for an operating system shell}}
{{about|scripting in Unix-like systems|batch programming in DOS, OS/2 and Windows|Batch file|batch in Windows PowerShell|Windows PowerShell#Scripting|programming in the Windows NT/2000 command shell|cmd.exe|command procedures on VAX/VMS systems|DIGITAL Command Language}}
[[File:FreeBSD 10 vi RC Firewall.png|thumb|Editing a [[FreeBSD]] shell script for configuring [[ipfirewall]] ]]
A '''shell script''' is a [[computer program]] designed to be run by a [[Unix shell]], a [[command-line interpreter]].<ref>{{ citation | last1 = Kernighan | first1 = Brian W. | author-link = Brian Kernighan | last2 = Pike | first2 = Rob | author2-link = Rob Pike | title = The UNIX Programming Environment | publisher = Prentice Hall, Inc. | year = 1984 | chapter = 3. Using the Shell | page = 94 | quote = The shell is actually a programming language: it has variables, loops, decision-making, and so on. | isbn = 0-13-937699-2 }}</ref> The various dialects of shell scripts are considered to be [[command language]]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]]), command procedures (VMS), and shell scripts ([[Windows NT]] stream and third-party derivatives like [[4NT (shell)|4NT]]—article is at [[cmd.exe]]), and mainframe operating systems are associated with a number of terms.<ref>{{cite web |title=Job Control Language |url=https://www.ibm.com/docs/en/zos/3.1.0?topic=introduction-job-control-language-jcl |access-date=2025-06-12 |publisher=IBM}}</ref>
All Unix-like systems include at least one [[POSIX]] shell (typically either [[Bash (Unix shell)|bash]] or the [[zsh]] compatibility mode),<ref>{{cite book|title=Classic Shell Scripting|author=Arnold Robbins and Nelson H.F. Beebe|year=2005|publisher=O'Reilly Media|isbn=978-0-596-00595-5|page=5}}</ref> while many also include a modern shell like [[Fish (Unix shell)|fish]] or [[nushell]].
==Capabilities==
===Comments===
[[Comment (computer programming)|Comments]] are ignored by the shell. They typically begin with the hash symbol (<code>#</code>), and continue until the end of the line.<ref name="Pro_Bash_Programming">{{cite book|last=Johnson|first=Chris|date=2009|url=https://books.google.com/books?id=NJmhi6T0nGMC&q=comment|title=Pro Bash Programming: Scripting the Linux Shell|publisher=Apress|access-date=September 27, 2019|isbn=9781430219989}}</ref>
===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 "<code>#!</code>".<ref name=Pro_Bash_Programming /> In Unix-like operating systems, the characters following the "<code>#!</code>" prefix are interpreted as a path to an executable program that will interpret the script.<ref>{{Cite web|url=https://linux.die.net/man/3/execve|title=exec(3p) – POSIX Programmer's Manual|access-date=2020-07-24}}</ref>
===Shortcuts===
One example would be to create a version of [[ls]], the command to list files, giving it a shorter command name of <
<
#!/bin/sh
LC_COLLATE=C ls -FCas "$@"
</syntaxhighlight>
Here,
The user could then simply use <
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
</syntaxhighlight>
In this case, the shell script would start with its normal starting line of <span style="font-family:courier">#!/bin/sh</span>. Following this, the script executes the command <span style="font-family:courier">clear</span> which clears the terminal of all text before going to the next line. The following line provides the main function of the script. The <span style="font-family:courier">ls -al</span> command
===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 [[
<
#!/bin/
cc -c foo.c
cc -c bar.c
cc -c qux.c
cc -o myprog foo.o bar.o qux.o
</syntaxhighlight>
The script would allow a user to save the file being edited, pause the editor, and then just run <
===Generalization===
Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users.
<
#!/bin/
for jpg; do # use $jpg in place of each filename given, in turn
png=
if convert "$jpg" jpg.to.png
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
exit 1
fi # the end of the "if" test construct
done # the end of the "for" loop
</syntaxhighlight>
The <
===
Many modern shells also supply various features usually found only in more sophisticated [[general-purpose programming language]]s, such as control-flow constructs, variables, [[Comment (computer programming)|comments]], arrays, [[subroutine]]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]] can also be embedded in shell scripts as can other scripting languages like [[Tcl]].<ref>{{cite book|title=Unix Shell Programming|author=Stephen G. Kochan, Patrick H. Wood|publisher=Sams Publishing|year=2003|isbn=9780672324901|page=243}}</ref> Perl and Tcl come with graphics toolkits as well.
== Typical shell languages ==
{{Main|Unix shell}}
Scripting languages commonly found on UNIX, Linux, and POSIX-compliant operating system installations include:
* The original [[Bourne shell]] (<code>sh</code>), though no longer in common use
* [[Bash (Unix shell)|GNU Bash]] (<code>bash</code>)
* [[Debian Almquist shell]] (<code>dash</code>)
* [[Z shell]] (<code>zsh</code>) if run in compatibility mode
Non-POSIX shells in common use include:
* Nushell (<code>nu</code>)
* [[Fish shell]] (<code>fish</code>)
* xonsh shell (<code>xonsh</code>), pronounced "conch", a [[Python (programming language)|Python]]-based shell
* [[PowerShell]] (<code>pwsh</code>)
The C and Tcl shells have syntax quite similar to that of said programming languages, and the Korn shells and Bash are developments of the Bourne shell, which is based on the [[ALGOL]] language with elements of a number of others added as well.<ref>Unix Shells By Example, pp 7-10,</ref> On the other hand, the various shells plus tools like [[awk]], [[sed]], [[grep]], and [[BASIC]], [[Lisp (programming language)|Lisp]], [[C (programming language)|C]] and so forth contributed to the [[Perl]] programming language.<ref>{{cite book |last1=Wall |first1=Larry |last2=Christiansen |first2=Tom |last3=Orwant |first3=Jon |title=Programming Perl |edition=5 |publisher=O'Reilly Media |year=2012 |isbn=9781449303587 |page=Preface}}</ref>
Formerly-popular shells include:
* [[Old shell]] (<code>osh</code>), a "port of the standard command interpreter from Sixth Edition UNIX"<ref>{{Cite web|url=https://manned.org/osh/f30afb07|title=osh - manned.org|website=manned.org|access-date=2019-01-16}}</ref>
* [[KornShell]] (<code>ksh</code>), since mostly replaced by its successor the [[Z shell]]
* [[Tenex C Shell]] (<code>tcsh</code>) and its predecessor the [[C shell]] (<code>csh</code>)
Related programs such as shells based on [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[C (programming language)|C]], [[Java (programming language)|Java]], [[Perl]], [[Pascal (programming language)|Pascal]], [[Rexx]] etc. in various forms are also widely available.
So called remote shells such as
* a [[Remote Shell]] (<code>rsh</code>)
* a [[Secure Shell]] (<code>ssh</code>)
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.<ref>{{cite book|last=Flanagan|first=David|title=JavaScript: The Definitive Guide|publisher=O'Reilly Media|year=2020|isbn=9781491952023|page=2}}</ref>
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.<ref>{{cite book|last=Harold|first=Elliotte Rusty|title=Java Network Programming|publisher=O'Reilly Media|year=2013|isbn=9781449365943|page=6}}</ref> Even when scripting languages such as Python or JavaScript support compilation to bytecode or use [[just-in-time compilation|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.<ref>{{cite book|last=Lutz|first=Mark|title=Learning Python|edition=5|publisher=O'Reilly Media|year=2013|isbn=9781449355739|page=6|quote=Python is often called a scripting language, but really it’s just a general-purpose programming language that’s also good at scripting. In fact, scripting is just a subset of programming in general.}}</ref>
==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 (programming language)|Python]], or [[C (programming language)|C]].
While files with the ".sh" [[file extension]] are usually a shell script of some kind, most shell scripts do not have any filename extension.<ref>{{cite book
|last1=Robbins |first1=Arnold |last2=Hannah |first2=Elbert |last3=Lamb |first3=Linda
|url=https://books.google.com/books?id=J5nKVVg5YHAC |title=Learning the vi and Vim Editors
|date=2008
|page=205
|publisher="O'Reilly Media, Inc." |isbn=9781449313258 }}</ref><ref>{{cite book
|last=Easttom |first=Chuck
|url=https://books.google.com/books?id=zZYLAAAAQBAJ |title=Essential Linux Administration:: A Comprehensive Guide for Beginners
|date=2012
|page=228
|publisher=Course Technology/Cengage Learning
|isbn=978-1435459571
}}</ref><ref>{{cite book
| title = Linux Shell Scripting Essentials
| last = Kumari
| first = Sinny
| quote = Rather than using a file extension for shell scripts, it's preferred to keep a filename without extension and let an interpreter identify the type by looking into shebang(#!).
| publisher = Packt Publishing Ltd
| date = November 23, 2015
| isbn = 9781783552375
| url = https://books.google.com/books?id=9vCoCwAAQBAJ&q=shell+script+file+extension&pg=PA2
| access-date = May 7, 2017
}}</ref><ref>{{cite book
| title = Wicked Cool Shell Scripts, 2nd Edition: 101 Scripts for Linux, OS X and UNIX Systems
| last1 = Taylor
| first1 = Dave
| last2 = Perry
| first2 = Brandon
| publisher = No Starch Press
| date = December 16, 2016
| isbn = 9781593276027
| quote = Shell scripts don't need a special file extension, so leave the extension blank (or you can add the extension .sh if you prefer, but this isn't required.
| url = https://books.google.com/books?id=Mpi7DQAAQBAJ&q=shell+script+file+extension&pg=PA5
| access-date = May 7, 2017
}}</ref>
==Advantages and disadvantages==
Perhaps the biggest advantage of writing a shell script is that the commands and syntax are exactly the same as those directly entered at the command
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.
On the other hand, shell scripting is prone to costly errors. Inadvertent typing errors such as <
Another significant disadvantage is the slow execution speed and the need to launch a new process for almost every shell command executed.
There are also compatibility problems between different platforms.
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 [[programming language syntax|language syntax]] or implementation, and are not necessarily imposed by the use of a text-based command
==Interoperability among scripting languages==
Many scripting languages share similar syntax and features due to their adherence to the [[POSIX]] standard, and several shells provide modes to emulate or maintain compatibility with others. This allows scripts written for one shell to often run in another with minimal changes.
For example, [[Bash (Unix shell)|Bash]] supports much of the original [[Bourne shell]] syntax and offers a POSIX-compliant mode to improve portability. However, Bash also includes a number of extensions not found in POSIX, commonly referred to as [[Bash (Unix shell)#Portability with POSIX|bashisms]]{{Broken anchor|date=2025-07-31|bot=User:Cewbot/log/20201008/configuration|target_link=Bash (Unix shell)#Portability with POSIX|reason= The anchor (Portability with POSIX) [[Special:Diff/1303454031|has been deleted]].|diff_id=1303454031}}. While these features enhance scripting capabilities, they may reduce compatibility with other shells like [[Dash (shell)|Dash]] or [[KornShell|ksh]].
==Shell scripting on other operating systems==
Interoperability software such as [[Cygwin]], the [[MKS Toolkit]], [[Interix]] (formerly part of Microsoft Windows Services for UNIX), [[Hamilton C shell]], and [[UWIN]] (AT&T Unix for Windows) enables Unix shell programs to run on Windows NT-based systems, though some features may not be fully supported on the older [[MS-DOS]]/[[Windows 95]] platforms. Earlier versions of the MKS Toolkit also provided support for OS/2.
In addition, several DCL (Digital Command Language) implementations are available for Windows environments. These include scripting tools like [[XLNT]], which integrates with the Windows command shell and supports automation for [[Common Gateway Interface|CGI]] programming and [[Windows Script Host]]. macOS (formerly Mac OS X) is also Unix-based and includes a POSIX-compliant shell environment by default.<ref>{{cite web |title=MacOS and the Unix Environment |url=https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/ |publisher=Apple Developer Documentation |access-date=2025-06-12}}</ref>
The console alternatives [[4DOS]], [[4OS2]], [[FreeDOS]], [[Peter Norton]]'s [[NDOS]] and [[Take Command Console)|4NT / Take Command]] which add functionality to the Windows NT-style cmd.exe, MS-DOS/Windows 95 batch files (run by Command.com), OS/2's cmd.exe, and 4NT respectively are similar to the shells that they enhance and are more integrated with the Windows Script Host, which comes with three pre-installed engines, VBScript, [[JScript]], and [[Visual Basic for Applications|VBA]] and to which numerous third-party engines can be added, with Rexx, Perl, Python, Ruby, and Tcl having pre-defined functions in 4NT and related programs. [[PC DOS]] is quite similar to MS-DOS, whilst [[DR DOS]] is more different. Earlier versions of Windows NT are able to run contemporary versions of 4OS2 by the OS/2 subsystem.
Scripting languages are, by definition, able to be extended; for example, a MS-DOS/Windows 95/98 and Windows NT type systems allows for shell/batch programs to call tools like [[KiXtart]], [[QBasic]], various [[BASIC]], [[Rexx]], [[Perl]], and [[Python (programming language)|Python]] implementations, the [[Windows Script Host]] and its installed engines. On Unix and other [[POSIX]]-compliant systems, [[awk]] and [[sed]] are used to extend the string and numeric processing ability of shell scripts. [[Tcl]], Perl, Rexx, and Python have graphics toolkits and can be used to code functions and procedures for shell scripts which pose a speed bottleneck (C, Fortran, assembly language &c are much faster still) and to add functionality not available in the shell language such as sockets and other connectivity functions, heavy-duty text processing, working with numbers if the calling script does not have those abilities, self-writing and self-modifying code, techniques like [[recursion]], direct memory access, various types of [[sorting]] and more, which are difficult or impossible in the main script, and so on. [[Visual Basic for Applications]] and [[VBScript]] can be used to control and communicate with such things as spreadsheets, databases, scriptable programs of all types, telecommunications software, development tools, graphics tools and other software which can be accessed through the [[Component Object Model]].
==See also==
* [[Glue code]]
* [[Interpreter directive]]
* [[Shebang (Unix)|Shebang symbol (#!)]]
* [[Unix shell]]s
* [[
* [[Windows Script Host]]
==References==
{{
==External links==
{{wikibooks|
* [http://www.faqs.org/docs/air/tsshell.html ''An Introduction To Shell Programming'' by Greg Goebel]
* [
* [
* [
* [http://freebookcentre.net/UnixCategory/Free-Unix-Shell-Programming-Books-Download.html Free Unix Shell scripting books]
* [https://help.ubuntu.com/community/Beginners/BashScripting Beginners/BashScripting], Ubuntu Linux
{{Programming languages}}
[[Category:Scripting languages]]
|