Category:UNIX Shell: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Implementation: --- Noting some common differences among different shells, and the "gotchyas" that arise from them)
m (Correct spelling.)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{language
[[Category:Solutions by Programming Language]]
|exec=interpreted

|tags=bash
The '''UNIX Shell''' is a component of terminal-based UNIX-derived systems which offers both a command-line interface for running system commands, as well as programming interface for intelligently automating tasks which use system commands.
|hopl id=568
}}The '''UNIX Shell''' is a component of terminal-based [[UNIX]]-derived systems which offers both a command-line interface for running system commands, as well as programming interface for intelligently automating tasks which use system commands.


=Implementation=
=Implementation=


There are many UNIX Shells and most of them can be categorized into two families. For purposes of the Rosette Code, all examples are in Bourne-compatible syntax. The other family of shells, with a markedly different syntax, are ''csh'' and it's ''tcsh'' (Tenex C Shell) "clone." Common Bourne compatible shells include the original [[Bourne Shell]] (''/bin/sh'' on most versions of UNIX), the GNU [[Bourne Again SHell]] (''bash'' --- which is linked to ''/bin/sh'' on many distributions of [[Linux]], making it their default shell), the [[Korn Shell]] (''ksh''), the [[Public Domain Korn SHell]] (''pdksh''), the [[Almquist SHell]] (''ash'') and the [[Debian Almquist SHell]] (''dash'') and the [[Z SHell]] (''zsh'').
There are many UNIX Shells and most of them can be categorized into two families. For purposes of the Rosetta Code, all examples are in Bourne-compatible syntax. The other family of shells, with a markedly different syntax, are ''csh'' ([[:Category:C Shell|C Shell]]) and its ''tcsh'' (Tenex C Shell) "clone." Common Bourne compatible shells include the original [[Bourne Shell]] (''/bin/sh'' on most versions of UNIX), the GNU [[Bourne Again SHell]] (''bash'' --- which is linked to ''/bin/sh'' on many distributions of [[Linux]], making it their default shell), the [[Korn Shell]] (''ksh''), the [[Public Domain Korn SHell]] (''pdksh''), the [[Almquist SHell]] (''ash'') and the [[Debian Almquist SHell]] (''dash'') and the [[Z SHell]] (''zsh'').

The original Bourne shell went through a number of revisions in the early years of UNIX, and support for some features varies considerably. By the time the SUSv3 (Single Unix Specification, version 3) features stablized. All recent versions of the various Bourne-compatible shells should support a common set of features, which are referred to throughout Rosette Code examples as "SUSv3" features. The Korn shell (originally written by David Korn of AT&T) and it's "public domain" clone offer extensions (such as co-processes, and "associative arrays" --- called "hash arrays" by Perl, "dictionaries" by Python, "maps" by Lua, etc).

Note that even when using a common subset of supported features there are subtle implementation differences, and in some cases parsing bugs, which can affect the portability of shell script examples. For example in ''bash'' before version 2.0 the following was tolerated:

{ echo foo; echo bar } ## Bug!!!

... though this is technically a bug in the language parsing. In ''bash'' versions newer than 2.0 this was fixed and the follow is required:

{ echo foo; echo bar; } ## Note the required semicolon

... (Or the ''}'' token can be put on a separate line)

Variations of this bug probably accounts for more "breakage" during upgrades of ''bash'' and when attempting to run ''bash'' scripts under other Bourne compatible shells than any other change in the history of Bourne-compatible shells.

Another common portability issue among different Bourne-compatible shells is a subtle matter of how pipe operations are handled. In all normal UNIX shells the '''''|''''' (pipe) operator creates a unidirectional inter-process communications (IPC) stream between one shell process and another. Thus a command like:

echo foo | read bar

... implicitly invokes a subshell (separate process) as either the producer or the consumer (writer into or reader from) this data"pipe."

The crucial different in semantics is determined by whether a given implementation of a shell creates the subshell/sub-process to the left or the right of the pipe operator. (Conceivably a shell could even create subprocesses on both sides of the operator). To demonstrate, and even test for, the difference run the following lines of code:

unset bar; echo "foo" | read bar; echo "$bar"

... shells such as ''ksh'' and ''zsh'' spawn their subshells to the left of the pipe ... so the sub-process is writing into the pipeline. This means that the existing process is reading values; thus the local shell variable "bar" is set after the second semicolon in this example. Under shells such as ''bash,'' ''ash,'' ''pdksh'' (and even in older versions of ''ksh'') the subshell is spawned on the right of the ''|'' operator. In those cases the ''read'' command is setting a value to a shell variable which ceases to exist after the second semicolon (which marks the end of that command, and thus the end of the completed sub-process.

To be portable such code must use command grouping:

unset bar; echo "foo" | { read bar; echo "$bar"; ...; }

... so that all of the commands after the pipe are executed within the same subshell.

Alternatively one could use an explicit shell sub-process (using the "parentheses" delimiters in lieu of the "brace" grouping operators), or one could re-structure the code using assignment and command substitution:


unset bar; bar=$(echo "foo"); echo "$bar" # some very old shells may require ` (backticks) instead of the $(...) syntax


''Main article: [[UNIX Shell Implementations]]''
Note that in all these examples the ''unset bar'' command is simply to avoid any confusion in the unlikely event that a variable named "bar" was present in the shell environment or local variable heap prior to our functional examples. This sort of difference, the implicit creation and scope of subshells and subproceses, and the underlying conceptual distinctions between shell and environment variables are at the root of many shell scripting portability issues and cause most of the confusion experienced by novices to UNIX shell scripting.


=Language=
=Language=


While UNIX Shells vary in the programming languages they support, such languages carry a minimum set of features. Each language allows the programmer to execute system commands as though he were typing the commands himself, and each language allows for a header line which specifies which shell implementation is used to interpret the script.
While UNIX Shells vary in the programming languages they support, such languages carry a minimum set of features. Each language allows the programmer to [[Execute a System Command|execute system commands]] as though he were typing the commands himself, and each language allows for a header line which specifies which shell implementation is used to interpret the script.


This one tells the operating system to use the [[Bourne Shell]]:
This one tells the operating system to use the [[Bourne Shell]]:
Line 56: Line 23:
#!/bin/ksh
#!/bin/ksh


Each header line consists of a hash, a bang, and the path to the interpreter binary.
Each header line consists of a hash, a bang, and the path to the [[interpreter]] binary.

Latest revision as of 17:13, 20 April 2017

Language
UNIX Shell
This programming language may be used to instruct a computer to perform a task.
Execution method: Interpreted
Lang tag(s): bash
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using UNIX Shell.

The UNIX Shell is a component of terminal-based UNIX-derived systems which offers both a command-line interface for running system commands, as well as programming interface for intelligently automating tasks which use system commands.

Implementation

There are many UNIX Shells and most of them can be categorized into two families. For purposes of the Rosetta Code, all examples are in Bourne-compatible syntax. The other family of shells, with a markedly different syntax, are csh (C Shell) and its tcsh (Tenex C Shell) "clone." Common Bourne compatible shells include the original Bourne Shell (/bin/sh on most versions of UNIX), the GNU Bourne Again SHell (bash --- which is linked to /bin/sh on many distributions of Linux, making it their default shell), the Korn Shell (ksh), the Public Domain Korn SHell (pdksh), the Almquist SHell (ash) and the Debian Almquist SHell (dash) and the Z SHell (zsh).


Main article: UNIX Shell Implementations

Language

While UNIX Shells vary in the programming languages they support, such languages carry a minimum set of features. Each language allows the programmer to execute system commands as though he were typing the commands himself, and each language allows for a header line which specifies which shell implementation is used to interpret the script.

This one tells the operating system to use the Bourne Shell:

#!/bin/sh

This line tells the operating system to use the Bourne Again SHell:

#!/bin/bash

And this one tells the operating system to use the Korn Shell:

#!/bin/ksh

Each header line consists of a hash, a bang, and the path to the interpreter binary.

Subcategories

This category has the following 5 subcategories, out of 5 total.

Pages in category "UNIX Shell"

The following 200 pages are in this category, out of 353 total.

(previous page) (next page)
(previous page) (next page)