Unix shell

from Wikipedia, the free encyclopedia

The Unix shell or shortly Shell ( English for cover, shell ) refers to the traditional user interface under Unix or Unix-like computer operating systems . The user can type commands in an input line , which the computer then immediately executes. One therefore speaks of a command line interpreter . In this context, the term shell was coined by Louis Pouzin in 1964/65 and his concepts were implemented by Glenda Schroeder for Multics .

As a rule, Unix users can choose between different shells. In terms of language scope, all common Unix shells can be used as full-fledged script languages for programming and automating tasks; The difference to pure script languages ​​(e.g. Perl , awk ) is that shells provide special means for interactive dialog with the user, from the output of a prompt in the simplest case to the possibility of editing the commands entered or for job control pass.

In contrast to the command line interpreters of some other operating systems (e.g. VMS ), Unix shells are ordinary user programs without special privileges.

General

After logging in, the shell is the program that is started by the loginprocess and receives commands. The commands are then carried out. A distinction is made between commands that are implemented in the shell, such as B. cd, and commands that are then searched for in the file system and started.

Most popular Unix shells offer the following features:

  • Starting commands
  • File name wildcards (globs) as command arguments
  • Conditions ( if, case) and loops ( while, for)
  • internal commands ( cd, read)
  • internal variables ( $HOME)
  • Manipulation of the environment variables for the new processes
  • Input / output redirection
  • Starting several processes, chaining via pipes
  • Starting processes in the background

Modern shells can also:

  • Completion of commands, file names and variables (completion system)
  • Editing the command line (command line editing)
  • Repetition and editing of previous commands (command history)
  • Stopping and restarting processes (job control)
  • Moving processes from the foreground to the background and vice versa (job control)
  • Built-in command for performing calculations ( $((2+2)))
  • Built-in command for testing file properties ( test)

With regard to the termination of a command, the shells make use of the properties of the underlying terminal device driver. The shell normally waits for a command to end. Strg+ Ccauses a command abort under Unix, which is triggered by the device driver.

Unix tools

Unix shells make use of the properties of the underlying operating system. You only implement the bare essentials. External Unix commands complement everything else via the powerful input / output and pipe implementation. Unix contains e.g. B. special commands for text file manipulation such as search, edit, replace, count words, character translation, truncation of lines, etc.

Scripts

Scripts are text files and represent small programs. They are written by the user and read and executed when called by the shell. Do you have to z. If, for example, you repeatedly type five commands into the shell in your day-to-day work, you can simplify your life by collecting these commands in a script and then just calling the entire script. The shell reads the script and executes the commands accordingly. It should be noted that all commands that are "manually" entered into the shell can also be executed via a script and vice versa.

A script that counts from one to a hundred could look like this:

#!/bin/sh
I=1                      # Variable I auf 1 setzen
while test $I -le 100    # While-Schleife, externes Kommando test I<=100
do
    echo $I              # externes Kommando echo, gibt I aus
    I=`expr $I + 1`      # externes Kommando expr, Zuweisung der Ausgabe von expr an I
done

Typed directly in the shell, it would look like this:

I=1; while test $I -le 100; do echo $I; I=`expr $I + 1`; done

In later versions of the Bourne shell and in more modern shells, the commands testand are echointernal, as they are needed very often and the shell no longer has to be kept small, as in very early systems.

The text of a script can be written to a file and chmod +xmade executable with the command . It then behaves like any other command, but the start of privileged scripts is often restricted.

If an executable Unix file begins with the character string #!( Shebang ), many operating systems interpret the following characters up to the end of the line as an indication of which program should be started in order to execute this file as a script. In this way, regardless of which shell the caller of a script interactively uses, you can ensure that the script is executed with the desired shell. However, this character string is not part of the POSIX standard.

System start

Shell scripts are also used when starting up the Unix system . The BSD variants simply start the script /etc/rcwhich then carries out all system initializations such as file system checks, background processes, etc. Use System V Unix variants sysinit, which consist of several shell scripts.

Commands that are actually shell scripts

Scripts cannot be distinguished from normal programs in use. Some Unix versions even provide some commands that are implemented as shell scripts. In particular, commands such as man(online manual) and cc(calling the C compiler, assembler and linker) are shell scripts in many Unix variants.

The early shells

The Thompson Shell

The Thompson shell was the first original Unix shell and was the standard shell of the UNIX system from AT&T between 1971 and 1979. Today's general syntax for redirecting input and output streams comes from this shell. The pipe concept was first implemented in 1973 in the Thompson Shell. In contrast to more recent shells, the Thompson shell was not a programming language. However , it was possible to control program sequences with an external ifand gotocommand. The Thompson shell was replaced as the standard shell by the Bourne shell in 1979.

A reimplementation of the original UNIX V6 shell for modern Unix-like systems is osh(for old shell ) or its successoretsh(for enhanced Thompson shell ).

The Bourne Shell

The ancestor of most of today's shells is the Bourne Shell (sh) by Stephen R. Bourne , which appeared in 1977/1978 together with Unix V7. A later version of the Bourne shell can still be found on almost all commercial Unix variants to this day, even if it is /bin/shincreasingly being replaced as a system shell ( ) by other shells. Version 7 of this shell already has all the essential properties such as input / output redirection, pipes , background processes and control structures. Regarding the command line editing, the shell relies on the terminal driver, which only allows the deletion of the entire line (CTRL-U) and the last character (DEL).

Over time, the original version has been expanded and changed. Since the Bourne shell never experienced a visible versioning, the respective variant can only be found out by testing the existing properties. The name then results from the variant of the AT&T Unix with which it was delivered (V7, System III, System V Release 1 - SVR1, SVR2, -3, -4, -4.2 for short).

The Bourne shell syntax is the basis of most modern Unix shells, which are essentially an extension of this shell. Scripts for the Bourne shell can also be run on these shells with practically no changes. The scripting abilities of the Bourne shell were trend-setting, and sh remains one of the most popular scripting languages ​​for Unix to this day. For this reason and for backward compatibility, a Bourne-compatible shell is generally available as / bin / sh .

Since Sun's OpenSolaris / SVR4 variant of the Bourne shell was published as "Open Source", this shell has now been available for almost all platforms thanks to a conversion of the memory management from sbrk(2)to malloc(3)by Jörg Schilling. A derivative of the Bourne shell ("POSIX shell") is the standard shell for new user accounts in FreeBSD .

Sample program

Although the range of functions of the Bourne shell is relatively small, all tasks typical for command line interpreters can be taken over with the help of standard Unix programs. The Bourne shell syntax is a bit idiosyncratic. Example:

#!/bin/sh
tageszeit=`date +%H`
if [ $tageszeit -lt 12 ]; then
  echo "Guten Morgen."
else
  echo "Guten Tag."
fi

In the first version of the Bourne shell, a mechanism was already implemented that meets the criteria e.g. B. could evaluate for an extended case distinction (case). The Bourne shell itself can only perform a Boolean evaluation. Other criteria are determined by external programs. (In recent Bourne shells is testimplemented and, like all built-in commands, used in the presence instead of external programs.) In the example, the built-in [call, which, apart from the fact that it played a ]are passed must be identical to test. The brackets are characterized semantic another notation for if test $tageszeit -lt 12the arguments current hour (stored in $tageszeit) -lt( less than - less than) 12. If the program testreturns the status 0 (ie "true"), everything thenbelonging to the instruction is executed. If testthe status returns 1 (ie "false"), everything elsebelonging to the instruction is executed. Since a list of instructions can follow the respective key words , the case distinction introduced by the key word is ended with the instruction fi(i.e. a reversed one if) if.

The C shell

In Berkeley, Bill Joy developed a shell for the second BSD distribution (2BSD) from 1979, which was more oriented towards the C syntax, the C shell (csh). This shell already allows command line editing, but not interactively, but using a special syntax. It also allows you to repeat (and edit) old commands. In addition, the job control option is being implemented for the first time: A command can be stopped using CTRL-Z and continued later using the internal commands fg(Foreground) or bg(Background).

The C shell has many well-known features that were later taken over by bash , such as: B. aliases or a history . The C shell is rarely used these days; it was replaced by other shells such as the tcsh, the ksh (Korn shell), the bash (Bourne again shell) or the zsh . With the Hamilton C shell there is a native implementation of the C shell for Microsoft Windows NT and earlier OS / 2 .

The scripting capabilities of the C shell are somewhat limited by various inadequacies, e.g. B. the error output cannot be diverted independently of the standard output. A user who uses the C shell for interactive input can use a prefix #!/bin/shin his scripts to cause the operating system to use the standard shell (this is at least compatible with the Bourne shell, on modern systems mostly POSIX- compliant) . This is also possible for all other shells.

Further developments

On every Unix system there is a shell compatible with the Bourne shell in / bin / sh . The availability of further shells depends on the respective Unix variant; a C shell version can often be found in / bin / csh . Because of the different syntax, a distinction is made between Bourne shell and C shell descendants. Since practically all modern shells are written in the C programming language , missing shells can easily be retrofitted. A Unix shell can also often be found on third-party operating systems, at least as third-party software.

The job control shell

The job control shell is a Bourne shell (more precisely the SVR4 variant, that is, first appeared on the AT&T Unix “System V Release 4”), which has job control properties (as known with the C shell have been expanded. These are often as much as the same program that the additional properties (eg. As the commands bg, fg, jobs, kill, stop, suspend, wait) is only activated when operating under the names jsh instead sh is called.

The Kornshell

David Korn developed the Kornshell (ksh) for Unix System V from AT&T . This is based on the Bourne shell, but also takes on the innovations of the C shell such as job control , a further improved command line editing. There is a version from 1988 (ksh88) and a newer version from 1993 (ksh93). The ksh88 is the basis of the POSIX standard. For a long time, the Korn shell was only commercially available as part of Unix System V. Since March 1st, 2000 the source code of ksh93 has been freely available. Many commercial Unix systems use the ksh as their default shell (/ bin / sh).

The dtksh ("Desktop Korn Shell") is a Korn shell with additional commands for programming user interfaces under X11 / Motif .

The public domain Korn Shell

The Public Domain Korn Shell (pdksh) is a free and not fully compatible copy of the AT&T Korn Shell. Many functions of ksh88 and a few of ksh93 are contained in it. The OpenBSD project uses a derivative of the pdksh as the standard shell (/ bin / sh).

The MirBSD Korn Shell (mksh) is based on OpenBSD's version of pdksh (oksh), but contains bug fixes and functions from other shells.

The Bourne Again Shell

The B ourne- a gain sh ell (bash) is part of the GNU project . Her name is an intentionally ambiguous play on words and can be interpreted as both “ born again shell” and “once again (a) Bourne shell”. Further interpretations are derived from the English bash ("the celebration", "the party", "the blow") and to bash ("hit", "criticize", "do badly"). Most of the bash was written by Brian Fox and Chet Ramey in the late 1980s.

Bash is largely compatible with the original Bourne shell (sh), but its range of functions has been considerably expanded. Above all, she has mastered a large part of the capabilities of the ksh and also understands parts of the syntax of the csh such as the command history, the directory stack, the $RANDOMvariable and the POSIX form of command substitution $(…). In addition, a number of own extensions have been implemented.

Bash (/ bin / bash) is the standard shell on most Linux systems as well as some versions of macOS and has been ported to almost all Unix systems.

There are numerous ports for Windows. The most well-known are:

The TENEX-C shell

The TENEX-C-Shell (tcsh) is an extension of the C-Shell (csh) by Christos Zoulas. It contains improvements of the command line editing and other extensions like a (programmable) filename completion, the possibility of editing files directly in the Command line, and some other things. Otherwise it is completely compatible with the C shell.

The 't' in tcsh is derived from the 'T' in TENEX , an operating system from which the author of the tcsh was inspired. The tcsh replaces the csh on many Linux systems, BSD systems and older versions of macOS.

The Z-Shell

The Z-Shell (zsh) is a shell with a lot of possibilities. It is very similar to the Korn shell and also takes over the functions of the Bourne Again shell and the TENEX-C shell. But it goes its own way in many respects and is considered one of the most powerful Unix shells. It can be used as a login shell, as an interactive shell and as an interpreter for shell scripts.

Zsh can be seen as a compilation of all the improvements and features of the bash, the csh and tcsh consider.

Some of their features include:

  • a freely programmable word completion (TAB-Completion)
  • the possibility to use the history from other - simultaneously running - shells
  • Spell check
  • almost complete compatibility with bash, ksh and tcsh
  • Strong changeability of the prompt through themes, etc. a. the ability to put the prompt on the right side of the terminal
  • extended globbing functionalities

The Z-Shell was developed by Paul Falstad and published on Usenet in 1990 . It is named after the login name zsh of one of his employees. In contrast to Bash, the zsh is under the BSD license , which is why it has replaced bash as the standard shell of macOS since macOS Catalina .

The Almquist Shell

The Almquist shell (ash) by Kenneth Almquist is a new implementation of the SVR4 variant of the Bourne shell. It was originally written for the traditional BSD variants of Unix, after the Bourne shell itself was only available with licensing. Later versions are based on the POSIX standard and are very suitable for executing scripts. The FreeBSD and NetBSD projects use evolved versions of ash as their default shell (/ bin / sh). Since the ash has very low demands on memory size and computer speed compared to other modern shells, it is also popular in embedded Linux systems ; it was also integrated into the multifunction binary BusyBox , which is also widely used in the embedded sector .

The Debian Almquist Shell

The Debian Almquist shell (dash), Bourne compatible and POSIX compliant, is a direct descendant of the Almquist shell. It was ported by Herbert Xu in 1997 and renamed dash in 2002. The dash is a modern replacement for the ash in the Debian project, where it is set up as / bin / sh , since it has advantages over the bash preferred in interactive use in terms of speed, robustness and number of dependencies. For the same reason, dash has been the default / bin / sh on Ubuntu since release 6.10.

More shells

  • In addition to a new implementation of the last Thompson shell (etshfor enhanced Thompson shell , formerlyoshalias old shell ), which was in use under Unix before the introduction of the Bourne shell, there is a Unix implementation (Byron Rakitzis) of the Plan 9 command interpreter rc (run command) by Tom Duff, which is syntactically clearer than the Bourne shell, and The semantically manipulable or expandable es (extensible shell) by Paul Haahr and Byron Rakitzis, which follows the syntax of the rc , flowed into the ideas of functional programming .
  • One shell in which the interactive aspect is initially deliberately neglected in favor of shell programming is the Scheme Shell (scsh) by Olin Shivers, with which a genuine programming language ( Scheme ) is available for shell programming .
  • A shell that is syntactically based on the Lisp programming language is esh (easy shell) by Ivan Tkatchev.
  • Axel Liljencrantz ' fish (friendly interactive shell) is primarily oriented towards interactive user-friendliness .
  • Gunnar Ritter made a port of the OpenSolaris Bourne Shell to Linux, Solaris, Free and NetBSD, which operates under the name Heirloom Bourne Shell .
  • Some Red Hat employees have developed the nash , which claims to be no shell ("not a shell"), but "only" a command interpreter that is supposed to interpret "linuxrc" images. It seems to be used mainly in the Red Hat and Mandriva environment. It is part of the mkinitrd package there.
  • The sash (stand-alone shell) is designed so that as many external commands as possible are replaced by the internal functions of the shell. This means that the system can also be operated when essential external programs are missing.
  • The Perl shells psh and psh2 are able to process Perl commands, for example to manipulate pipes efficiently.
  • The IPython program provides an interactive shell based on the Python programming language .

Compatibility and Portability

Depending on the origin of the shell, scripts written for them are incompatible or only partially compatible with other shells. For example, Bourne shell scripts cannot be run under the C shell, but there is a certain chance that such a script will run under a Korn shell or job control shell. In addition to this obvious problem, there is also the problem that different implementations of the same shell exist and that shells have been and will be further developed over time while retaining their name. The same (identically named) shells on different Unix variants have different development statuses and thus both different properties and errors. In addition, as already described, shell scripts call many normal Unix commands. These too differ in their behavior in part from Unix to Unix. This can also change the behavior of a shell script when it is run on another Unix.

A common strategy for writing portable shell scripts is to rely on the lowest common denominator. This usually means that you use the Bourne shell and thus deliberately do without the convenient extensions such as those found in the Korn shell or bash. Depending on how large the bandwidth of the systems to be covered is, the use of newer Bourne shell features standardized in POSIX is also avoided.

literature

  • Helmut Herold: Linux-Unix-Shells, Bourne-Shell, Korn-Shell, C-Shell, bash, tcsh. Addison-Wesley, Munich 2003, ISBN 3-8273-1511-5 .
  • Alexander Mayer: Shell scripts in Unix. C&L - Computer & Literatur-Verlag, Böblingen 2005, ISBN 3-936546-23-1 .
  • Morris I. Bolsky, David G. Korn: The new Korn Shell Command and Programming Language. 2nd Edition. Prentice Hall, Upper Saddle River 1995, ISBN 0-13-182700-6 .
  • Arnold Robbins, Bill Rosenblatt: Learning the Korn Shell, 2nd Edition. O'Reilly, Beijing 2002, ISBN 0-596-00195-9 .
  • Paul Dubois: Using tcsh and csh. O'Reilly, Beijing 1995. ISBN 1-56592-132-1
  • Patrick Ditchen: Shell script programming. 2nd Edition. mitp, Heidelberg 2006, ISBN 3-8266-1623-5 .
  • Ellie Quigley: UNIX Shells by Example, 4th Edition. Prentice Hall PTR, Upper Saddle River 2005, ISBN 0-13-147572-X .
  • O. Kiddle, J. Peek, P. Stephenson: From Bash to Z Shell. Apress, New York 2004, ISBN 1-59059-376-6 .
  • Jürgen Wolf: Shell programming - introduction, practice, reference. Galileo Computing, Bonn 2005, ISBN 3-89842-683-1 .
  • Sven Guckes, Julius Plenz: Zsh - The magical shell. Open Source Press, Munich 2008, ISBN 3-937514-59-7 .

Web links

Wikibooks: Linux practical book: Shell programming  - learning and teaching materials

Individual evidence

  1. Michael Urban, Brian Tiemann: Sams teach yourself FreeBSD in 24 hours . Sams Publishing, 2002, ISBN 978-0-672-32424-6 , pp. 56 ( limited preview in Google Book search).
  2. bash gnu.org
  3. nash ( memento of the original from January 20, 2009 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. -man-page @1@ 2Template: Webachiv / IABot / gd.tuwien.ac.at
  4. psh2
  5. ^ IPython as a system shell. Retrieved February 23, 2018 .