Shell script: Difference between revisions

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 programming in Windows PowerShell shell|Windows PowerShell#Scripting}}
{{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}}
{{ref improve|date=February 2014}}
[[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>
A '''shell script''' is a [[computer program]] designed to be run by the [[Unix shell]], a [[command line interpreter]].<ref>{{ citation | last = Kernighan | first = 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 [[scripting language]]s.
 
Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
 
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===
In its most basic form, aA 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 <ttcode>l</ttcode>, which would be normally saved in a user's <ttcode>bin</ttcode> directory as <ttcode>/home/''username''/bin/l</ttcode>, and a default set of command options pre-supplied.
 
<sourcesyntaxhighlight lang="bashsh">
#!/bin/sh
LC_COLLATE=C ls -FCas "$@"
</syntaxhighlight>
</source>
 
Here, [[Shebang (Unix)|the first line (Shebang)uses a [[#Configurable choice of scripting language|shebang]] indicatesto 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 <ttcode>LC_COLLATE=C</ttcode> sets the default collation order to not fold upper and lower case together, not intermix [[dotfile]]s with normal filenames as a side effect of ignoring punctuation in the names (dotfiles are usually only shown if an option like <ttcode>-a</ttcode> is used), and the <ttcode>"$@"</ttcode> causes any parameters given to <ttcode>l</ttcode> to pass through as parameters to ls, so that all of the normal options and other [[programming language syntax|syntax]] known to ls can still be used.
 
The user could then simply use <ttcode>l</ttcode> 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.
 
<sourcesyntaxhighlight lang="bashsh">
#!/bin/sh
 
clear
ls -al
</syntaxhighlight>
</source>
 
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 listlists the files and directories that are in the directory from which the script is being run. The <span style="font-family:courier">[[ls]]</span> command attributes could be changed to reflect the needs of the user.
 
Note: If an implementation does not have the <span style="font-family:courier">clear</span> command, try using the <span style="font-family:courier">clr</span> 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 [[C shellPOSIX]]-compliant scriptshells, here named <ttcode>build</ttcode> and kept in the directory with them, which would compile them automatically:
 
<sourcesyntaxhighlight lang="bashsh">
#!/bin/cshsh
echoprintf 'compiling...\n'
cc -c foo.c
cc -c bar.c
cc -c qux.c
cc -o myprog foo.o bar.o qux.o
echoprintf 'done.\n'
</syntaxhighlight>
</source>
 
The script would allow a user to save the file being edited, pause the editor, and then just run <ttcode>./build</ttcode> 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 (software)|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 [[Bash (UnixPOSIX shell)]]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 <ttcode>/home/''username''/bin/jpg2png</ttcode>
 
<sourcesyntaxhighlight lang="bash" enclose="divsh">
#!/bin/bashsh
for jpg; do # use $jpg in place of each filename given, in turn
png="${jpg%.jpg}.png" # findconstruct the PNG version of the filename by replacing .jpg with .png
echoprintf 'converting "$jpg%s" ...\n' "$jpg" # output status info to the user running the script
if convert "$jpg" jpg.to.png ; then # use the convert program (commonprovided inby LinuxImageMagick) 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
echoprintf >&2 'jpg2png: error: failed output saved in "jpg.to.png".\n' >&2
exit 1
fi # the end of the "if" test construct
done # the end of the "for" loop
echoprintf 'all conversions successful \n' # tell the user the good news
</syntaxhighlight>
exit 0
</source>
 
The <ttcode>jpg2png</ttcode> command can then be run on an entire directory full of JPEG images with just <ttcode>/home/''username''/bin/jpg2png *.jpg</ttcode>
 
===VerisimilitudeProgramming===
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.
A key feature of shell scripts is that the invocation of their interpreters is handled as a core operating system feature. So rather than a user's shell only being able to execute scripts in that shell's language, or a script only having its [[interpreter directive]] handled correctly if it was run from a shell (both of which were limitations in the early Bourne shell's handling of scripts), shell scripts are set up and executed by the OS itself. A modern shell script is not just on the same footing as system commands, but rather many system commands are actually shell scripts (or more generally, scripts, since some of them are not interpreted by a shell, but instead by [[Perl]], [[Python (programming language)|Python]], or some other language). This extends to returning exit codes like other system utilities to indicate success or failure, and allows them to be called as components of larger programs regardless of how those larger tools are implemented.
 
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.
Like standard system commands, shell scripts classically omit any kind of filename extension unless intended to be read into a running shell through a special mechanism for this purpose (such as <tt>sh</tt>’s “<code>'''. '''</code>”, or <tt>csh</tt>’s <tt>source</tt>).
 
== Typical shell languages ==
===Programming===
{{Main|Unix shell}}
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.
 
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==
{{mainMain|scriptingScripting language}}
 
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>
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]]s is a frequent source of debate. But generally speaking a scripting language is one which requires an interpreter.
<!-- this bit seems speculative, as there are often many other reasons cited: "However, core system maintenance scripts, which might otherwise be written in such a language, continue to be written as shell scripts, as they cannot always rely on the presence of the relevant scripting language engines." -->
 
==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]]. 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.
 
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGgAigMBIgACEQEDEQH/xAAcAAEA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zeDD/AIYuR935HYKBe2ldJO+WvEXTP81i0fEefpyH6rXy4kkkkk8yvK4WJPmntnbyPWp4JFu7ZuO3rM75OxPAew5BWFRFauCs9xyPidvRvcw9WnClx6tqEYw23L9Tn/lQUUNJ9k7ZIsXbVn9/YkeOhdw+ysKiKVwQEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREB/9k=">
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 -line. The programmer does not have to switch to a totally different syntax, as they would if the script were written in a different language, or if a compiled language waswere used.
 
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 re-run 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 <ttcode>[[rm (Unix)|rm]] -rf * /</ttcode> (instead of the intended <ttcode>rm -rf */</ttcode>) are folklore in the Unix community; a single extra space converts the command from one that deletes everythingall subdirectories contained in the sub-directoriescurrent directory, to one which deletes everything—andeverything alsofrom triesthe tofile delete everything in thesystem's [[root directory]].<ref>{{cite book|last=Shotts|first=William|title=The Linux Command Line|publisher=No Starch Press|year=2019|isbn=9781593279523|page=72}}</ref> Similar problems can transform <ttcode>[[cp (Unix)|cp]]</ttcode> and <ttcode>[[mv (Unix)|mv]]</ttcode> into dangerous weapons, and misuse of the <ttcode>></ttcode> redirect can delete the contents of a file.<ref>{{cite book This|last1=Albing is|first1=Carl made|last2=Vossen more|first2=JP problematic|last3=Newham by|first3=Cameron the|title=Bash factCookbook that|publisher=O'Reilly manyMedia UNIX commands differ in name by only one letter: <tt>cp</tt>, <tt>[[cd (command)|cd]]</tt>,year=2007 <tt>[[dd (Unix)|dd]]</tt>,isbn=9780596526788 <tt>[[df (Unix)|df]]page=53}}</ttref>, etc.
 
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 (computing)|pipeline]] in which efficient [[Filter (software)|filter]] 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]], creator of [[Perl]], famously wrote that "It is's easier to port a shell than a shell script."<ref>{{cite newsgroup |title=Finding the last arg |author=[[Larry Wall]] |date=January 4, 1991 |newsgroup=comp.unix.shell |url=https://www.tuhs.org/Usenet/comp.unix.shell/1991-January/002464.html |access-date=January 5, 2023}}</ref>
 
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. <ref>[http{{cite web|url=https://www.ooblick.com/text/CshProgrammingConsideredHarmful.html "|title=Csh Programming Considered Harmful"]|author=Christiansen, Tom}}</ref>
 
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 -line; there are a number of shells which use other shell programming languages or even full-fledged languages like [[Scsh]] (which uses [[Scheme (programming language)|Scheme]]).
 
==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 PowerShell]]
* [[Windows Script Host]]
 
==References==
{{reflistReflist}}
 
==External links==
{{wikibooks|AdShell Hoc Data Analysis From The Unix Command LineProgramming}}
* [http://www.faqs.org/docs/air/tsshell.html ''An Introduction To Shell Programming'' by Greg Goebel]
* [httphttps://steve-parkerwww.shellscript.org/sh/sh.shtml ''UNIX / Linux shell scripting tutorial'' by Steve Parker]
* [httphttps://developer.apple.com/mac/library/documentation/OpenSource/Conceptual/ShellScripting/ ''Shell Scripting Primer'' (Apple)]
* [httphttps://www.linux.com/articles/34658 ''What to watch out for when writing portable shell scripts'' by Peter Seebach]
* [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]]