Bash (Unix shell): Difference between revisions

Content deleted Content added
Blush30720 (talk | contribs)
Features: add a feature description
 
(30 intermediate revisions by 8 users not shown)
Line 150:
| url-status = dead
}}
</ref>
 
Since its inception, Bash has gained widespread adoption and is commonly used as the default [[login]] shell for numerous [[Linux]] distributions. It holds historical significance as one of the earliest programs ported to Linux by [[Linus Torvalds]], alongside the GNU Compiler ([[GNU Compiler Collection|GCC]]).<ref name="groups.google.com">
Line 164:
</ref> It is available on nearly all modern operating systems, making it a versatile tool in various computing environments.
 
As a [[Command-line interface|command processor]], Bash operates within a [[Terminal emulator|text window]] where users input commands to execute various tasks. It also supports the execution of commands from files, known as [[shell script]]s, facilitating [[automation]]. In keeping with [[Unix shell]] conventions, Bash incorporates a rich set of features. The [[Keyword (computer programming)|keywords]], [[Syntax (programming languages)|syntax]], [[Scope (computer science)#Dynamic scoping|dynamically scoped]] variables and other basic features of the [[formal language|language]] are all copied from the [[Bourne shell]], (`<code>sh`)</code>. Other features, e.g., [[C shell#History|history]], are copied from the [[C shell]], (`<code>csh`)</code>, and the [[Korn Shell]], (`<code>ksh`)</code>. Bash is a [[POSIX]]-compliant shell, but with a number of extensions.
 
== History ==
While bashBash iswas considereddeveloped Unixfor UNIX and UNIX-like operating systems such as GNU/Linux,<ref>
{{ cite web
| access-date = April 9, 2016
Line 177:
| url-status = dead
}}
</ref> it's is also available on MacOS, Windows, BeOS,<ref name = "forb">
{{ cite web
| title = A desktop alternative
Line 469:
As a [[Command-line interface|command processor]], Bash operates within a [[Terminal emulator|text window]] where users input commands to execute various tasks. It also supports the execution of commands from files, known as [[shell script]]s, facilitating [[automation]]. In keeping with [[Unix shell]] conventions, Bash incorporates a rich set of features, including:
 
* [[Control flow|Control structures]] for [[ConditionalFunction (computer programming)|condition-testingInvocation]] andas [[iteration]];a...
** [[Interactivity|Interactive]] shell,
* [[Unix|UNIX]]-style [[Pipeline (Unix)|pipelines]];
** Non-interactive shell, or
* [[Child process|Subshells]];
** Login shell;
* A [[command-line interface]];
* [[Exit status]] codes;
* [[Control flow|Control structures]] for
* [[Signal (IPC)|Signaling]] as a means of [[inter-process communication]];
** [[Conditional (computer programming)|Condition testing]],
* Asynchronous execution;
*** <code>if</code>, <code>case</code>, <code>select</code>,
*** logical AND (<code>&&</code>) and OR (<code>||</code>), and
** [[Iteration]]
*** <code>for</code>, <code>while</code>, <code>until</code> loops, and
*** Arithmetic C-style loop: <code>for ((</code>;
* Syntaxes for file type, string value and integer value testing
** Traditional single bracket test: <code>[</code>,
** Modern double bracket test: <code> &#91;&#91; ... &#93;&#93;</code>, including
*** Extended [[regular expression]] and extglob matching
*** Lexicographic sorting with <code><</code> and <code>></code>;
* [[Unix|UNIX]]-style [[Pipeline (Unix)|pipelines]]: <code>|</code>;
* [[Child process|Subshells]]: <code>( ... )</code>;
* [[Signal (IPC)|Signaling]] as a means of [[inter-process communication]] using the <code>trap</code> builtin;
* Asynchronous execution: <code>job_spec &</code>;
* A shell [[Porting|portability]] mode where commands can be interpreted in conformance with the [[POSIX terminal interface|POSIX]] standard;
* [[Function (computer programming)|Invocation]] as a...
** Login shell,
** [[Interactivity|Interactive]] shell, or
** Non-interactive shell;
* Command parsing:
** Comments are ignored:
*** Bourne-style <code>#</code> hashtag comments, and
*** Thompson-style <code>:</code> colon comments;
** Commands are parsed one line at a time,
*** Control structures are honored, and
** Split into words according to [[String literal|quoting]] rules,
*** Backslash <code>\</code> escapes are also honored at the ends of lines,
*** Including ANSI-C quoting, and
** Split into words (i.e., [[Text segmentation|word splitting]]) according to [[String literal|quoting]] rules,
** Comments are ignored.
*** Including ANSI-C quoting <code>$'...'</code>, and
* A series of expansions are performed on the resulting string:
** Seven kinds of expansions are performed on the resulting string in the following order:
** Brace expansion,
*** Tilde(Step 1) Brace expansion <code>kernel{-headers}</code>,
*** (Step 2) Tilde expansion <code>~</code>,
** [[Parameter (computer programming)|Parameter]] and [[Variable (computer science)|variable]] expansion, including
*** (Step 3) In a left-to-right fashion:
*** [[Scope (computer science)#Dynamic scoping|Dynamically scoped]] variables,
**** [[Parameter (computer programming)|Parameter]] and [[Variable (computer science)|variable]] expansion <code>$foo</code> or <code>${bar}</code>, including
*** Indexed [[Array (data type)|arrays]] of unlimited size, and
***** [[Scope (computer science)#Dynamic scoping|Dynamically scoped]] variables,
*** [[associative array|Associative arrays]],
***** Indexed [[Array (data type)|arrays]] of unlimited size,
** [[command substitution|Command substitution]],
***** [[Associative array]]s via <code>declare -A</code>, and
** [[Process substitution|Process substitution]],
***** Expansion syntaxes which can perform some tasks more quickly than external utilities, such as
** Arithmetic expansion, including
****** Pattern Substitution
*** Integer [[Arithmetic|arithmetic]] in any [[Radix|base]] from two to sixty-four,
******* <code>${foo//x/y}</code> for <code>sed 's/x/y/g'</code>,
** [[Text segmentation|Word splitting]] (again),
****** Remove Matching Prefix or Suffix Pattern
** Pathname expansion,
******* <code>${bar##[a-zA-Z0-9]*}</code> for <code>cut -c8-</code>,
*** I.e., [[Glob (programming)|globbing]] and [[pattern matching]], and
****** Print Array Keys
** [[String literal|Quote]] removal;
******* <code>${!array[@]}</code>, and
* [[Redirection (computing)|Redirections]] of inputs, outputs and error data streams are performed, including [[here document|here documents]] and here strings;
****** Display Error if Null or Unset
* Command name lookup is performed, in the following order:
******* <code>${var:?error message}</code>, among others,
** [[Command (computing)|Commands]] internal to the [[Shell (computing)|shell]]:
**** [[Command substitution]]: <code>$( ... )</code>,
*** [[Alias (command)|Aliases]],
**** [[Process substitution]], <code><()</code> or <code>>()</code>, when a system supports it:
*** Shell reserved words,
**** Arithmetic expansion, <code>(( ... ))</code> or <code>$(( ... ))</code>, including
*** [[Function (computer programming)|Functions]], and
***** Integer [[arithmetic]] in any [[Radix|base]] from two to sixty-four, although
*** [[Shell builtin|built-in commands]];
***** [[Floating-point arithmetic]] is not available from within the shell itself (for this functionality, see current versions of [[Bc (programming language)|<code>bc</code>]] and [[AWK|<code>awk</code>]], among others),
** [[Command (computing)|Commands]] external to the [[Shell (computing)|shell]]:
*** (Step 4) [[Text segmentation|Word splitting]] (again),
*** Separate [[Unix|UNIX]]-style programs such as `[[ls]]` or `[[Ln (Unix)|ln]]`, and
*** (Step 5) Pathname expansion,
*** [[shell script|Shell scripts]], which are [[File system|files]] containing executable commands. (Shell scripts do not require compilation before execution and, when certain requirements are met, can be invoked as commands by using their filename.)
**** Meaning shell-style [[Glob (programming)|globbing]] and [[pattern matching]] using <code>*</code>, <code>?</code>, <code>[...]</code>, and
* The resulting string is executed as a command.
*** [[String literal|Quote]] removal;
** [[Redirection (computing)|Redirections]] of Standard Input, Standard Output and Standard Error [[Standard streams|data streams]] are performed, including
*** File writing, <code>></code>, and appending, <code>>></code>,
*** [[Here document]]s, <code><<</code>,
*** Here strings, <code><<<</code>, which allow parameters to be used as input, and
*** A redirection operator, <code>>|</code>, which can force overwriting of a file when a shell's "noclobber" setting is enabled;
** Command name lookup is performed, in the following order:
*** [[Command (computing)|Commands]] internal to the [[Shell (computing)|shell]]:
**** [[Alias (command)|Shell aliases]],
**** Shell reserved words,
**** [[Function (computer programming)|Shell functions]], and
**** [[Shell builtin|Shell built-in commands]];
*** Commands external to the shell:
**** Separate [[Unix|UNIX]]-style programs such as [[ls|<code>ls</code>]] or [[Ln (Unix)|<code>ln</code>]], and
**** [[Shell script]]s, which are [[File system|files]] containing executable commands. (Shell scripts do not require compilation before execution and, when certain requirements are met, can be invoked as commands by using their filename.)
** The resulting string is executed as a command.
 
Bash also offers...
 
* Configurable execution environment(s):<ref>
{{ cite web
| title = Command Execution Environment (Bash Reference Manual)
| url = https://www.gnu.org/software/bash/manual/html_node/Command-Execution-Environment.html
| website = www.gnu.org}}
</ref>:
** Shell and [[Session (computer science)|session]] startup files such as '<code>~/.bashrc'</code> and <code>~/.profile’profile</code> (i.e., [[Hidden file and hidden directory|dotfiles]]);
* Settings (`<code>set`</code> [[Shell builtin|built-in]]) and shell options (`<code>shopt`</code> [[Shell builtin|built-in]]) which alter shell behavior;
* With interactive invocation only,
** Unlimited size [[Command history|command history]],
** [[Job (computing)|Jobs]] and [[Job control (Unix)|job control]],
** A directory stack (see <code>pushd</code> and <code>popd</code> built-ins),
** [[Command-line completion|Tab completion]],
** Configurable [[Command-line interface#Command prompt|prompts]], and
Line 531 ⟶ 563:
* Shell [[Compatibility mode|compatibility modes]]: bash 5.1 can operate as if it were bash 4.2, etc.;
* Documentation:
** A built-in `<code>help`</code> command,
** A [[man page]], and
** An [[Info (Unix)|info page]] which is the same as the GNU manual;
Line 538 ⟶ 570:
** Mailing lists at https://www.gnu.org/software/bash/
 
The [[Keyword (computer programming)|keywords]], [[Syntax (programming languages)|syntax]], [[Scope (computer science)#Dynamic scoping|dynamically scoped]] variables and other basic features of the [[formal language|language]] are all copied from [[Bourne shell|sh]]. Other features, e.g., [[C shell#History|history]], are copied from [[C shell|csh]] and [[KornShell|ksh]].
 
The [[Keyword (computer programming)|keywords]], [[Syntax (programming languages)|syntax]], [[Scope (computer science)#Dynamic scoping|dynamically scoped]] variables and other basic features of the [[formal language|language]] are all copied from [[Bourne shell|sh]]. Other features, e.g., [[C shell#History|history]], are copied from [[C shell|csh]] and [[KornShell|ksh]].
 
The Bash [[command (computing)|command]] syntax is a [[superset]] of the Bourne shell command syntax. Bash supports [[brace expansion]],<ref>
Line 666 ⟶ 697:
| website = www.gnu.org
}}
</ref> They can be used to emulate multidimensional arrays. Bash 4 also switches its license to [[GNU General Public License#Version 3|GPL-3.0-or-later]]; some users suspect this licensing change is why MacOSmacOS continues to use older versions.<ref>
{{ cite web
| access-date = June 25, 2018
Line 676 ⟶ 707:
| website = Ask Different
}}
</ref> Apple[[Z finallyshell|Zsh]] stoppedbecame usingthe Bashdefault shell in its operating systems as default shellmacOS with the release of MacOSmacOS Catalina in 2019.<ref name=":0">
{{ cite web
| access-date = 2021-01-12
Line 750 ⟶ 781:
===Startup scripts===
 
{{ SectionHow-to howtosection | date=January 2019 }}
 
When Bash starts, it executes the commands in a variety of [[Hidden file and hidden directory#Unix and Unix-like environments|dot files]]. Unlike Bash shell scripts, dot files do typically have neither the execute permission enabled nor an [[interpreter directive]] like <code>#!/bin/bash</code>.
Line 804 ⟶ 835:
* Bash-specific builtins
* [[Coprocess]]es
* $EPOCHSECONDS and $EPOCHREALTIME variables <ref>
{{ cite web
| access-date = June 3, 2020
Line 859 ⟶ 890:
In this example, when command1 is finished, command2 is executed, and when command2 has completed, command3 will execute.
 
A [[Background process|background execution]] of command1 can occur using (symbol &) at the end of an execution command, and process will be executed in background while returning immediately returning control to the shell and allowing continued execution of commands.
<syntaxhighlight lang="bash">command1 &</syntaxhighlight>
 
Or to have a concurrent execution of two command1 and command2, they must be executed in the Bash shell in the following way:
 
<syntaxhighlight lang="bash">
Line 959 ⟶ 990:
 
=== Program name ===
{{OriginalResearchOriginal research|section|date=May 2024|discuss=Talk:Bash (Unix shell)#Possible original research}}
The program's name is a [[figure of speech]] or [[wit]]ticism which begins with an [[Homage (arts)|homage]] to [[Stephen R. Bourne|Stephen Bourne]], the creator of [[Bourne shell|one]] of the shell programs which have sometimes been considered superseded by the bash shell. His name is used as a [[pun]] on the [[Imagery|image]] of childbirth. With that pun, it would seem, is added an [[allusion]]: possibly to the [[Hinduism|Hindu]] or [[Buddhism|Buddhist]] idea of [[reincarnation]]; possibly to the [[Christianity|Christian]] [[idiom]] known as "being [[born again]];" or quite possibly just to the more abstract idea of [[wikt:renewal|renewal]]. While numerous [[English language|English]] [[translation]]s of the Christian [[New Testament]], Book of [[Gospel of John|John]], [https://www.biblegateway.com/passage/?search=John%203&version=NIV chapter] [https://www.biblegateway.com/passage/?search=John%203&version=KJV 3] do contain the words "born again," [[Merriam-Webster|Merriam-Webster's]]'s dictionary has "born-again" defined as a "...person who has made a renewed or confirmed ''commitment''....<ref>
{{ cite web
| access-date = 2024-01-09
Line 982 ⟶ 1,013:
| website = www.gnu.org
}}
</ref>
 
The [[acronym]] of that name then is "bash," a word meaning "to strike violently.<ref>
Line 993 ⟶ 1,024:
| website = www.merriam-webster.com
}}
</ref>" In the context of computer programming, to "violently hit something," such as a computer keyboard, could be considered a [[Hyperbole|hyperbolic]] [[Imagery|image]] of some ''frustration''. Such [[imagery]] of negative emotionality could be seen as standing in direct [[juxtaposition]] to the idea of becoming "born again."
 
The naming could be considered an instance of verbal [[irony]]<ref>
Line 1,053 ⟶ 1,084:
| url = https://wiki.bash-hackers.org/scripting/obsolete
}}
</ref> so much so that it seems likely that the bash project has been ''committed'' to improving its usabliltyusability. Since then, bash has become the de facto default shell program in most Linux and Unix operating systems.
 
=== Documentation ===
Line 1,076 ⟶ 1,107:
| website = git.savannah.gnu.org
}}
</ref> is intended to be the authoritative explanatory document for the understanding of how `<code>bash`</code> operates, while the GNU [https://www.gnu.org/software/bash/manual/index.html manual] is sometimes considered more user-friendly for reading. "You may also find information about Bash by running <kbd>info bash</kbd> ... or by looking at <code>/usr/share/doc/bash/</code>, <code>/usr/local/share/doc/bash/</code>, or similar directories on your system. A brief summary is available by running <kbd>bash --help</kbd>.<ref name="auto"/>"
 
On modern Linuxes, information on shell built-in commands can be found by executing <code>help,</code> <code>help [built-in name]</code>or `<code>man builtins`</code> at a terminal prompt where bash is installed. Some commands, such as `<code>echo`</code>, `<code>false`</code>, `<code>kill`</code>, `<code>printf`</code>, `<code>test`</code> or `<code>true`</code>, depending on your system and on your locally installed version of bash, can refer to either a shell built-in or a system binary executable file. When one of these command name collisions occurs, bash will by default execute a given command line using the shell built-in. Specifying a binary executable's absolute path (i.e., `<code>/bin/printf`</code>) is one way of ensuring that the shell uses a system binary. This name collision issue also effects any "help summaries" viewed with `<code>kill --help`</code> and `<code>/bin/kill --help`</code>. Shell built-ins and system binary executable files of the same name often have differing options.
 
"The project maintainer also has a Bash page which includes Frequently Asked Questions",<ref>
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy