Bash (Unix shell): Difference between revisions

Content deleted Content added
m tidy remaining fsf refs
m fix quot; repair
 
(41 intermediate revisions by 7 users not shown)
Line 1:
{{Short description|GNU replacement for the Bourne shell}}
{{multiple issues|
<!--{{In use| date = 2025-08-12| section = entire article}}-->
{{Under constructionhow-to|date=August 2025-08-12|nosection=yes}}
{{More refs|date=August 2025}}
{{Over-quotation|date=August 2025}}
}}
{{Use dmy dates|date=March 2014}}
{{Infobox software
Line 265 ⟶ 267:
| url = https://developer.mozilla.org/en-US/docs/Glossary/Character
| website = mozilla.org
| date = 11 July 2025
}}
</ref>
Line 333 ⟶ 336:
| quote = UNIX is case sensitive. Because UNIX is case sensitive, our shell scripts are also case sensitive.
| title = Mastering Unix Shell Scripting, 2e
| url = https://books.google.com/books?hl=en&lr=&id=uI7C2BDF6F0C
}}
</ref>
Line 362 ⟶ 365:
BCPL is a precursor of the [[C (programming language) |C programming language]], in which Bash is written.
In [[C++]], a descendant of C, it's also true that "arrays start indexing from element 0."<ref>
{{Cite webbook
| access-date = 25 August 2025
| author = Graham M. Seed
Line 370 ⟶ 373:
| publisher = [[Springer Science+Business Media]]
| title = An Introduction to Object-Oriented Programming in C++, 7.4 Array Indexing
| url = https://books.google.com/books?id=_lqj98AsnGAC&q=zeroth+element&pg=PA195#v=snippet&q=zeroth%20element&f=false
| isbn = 978-1-85233-450-5
| website = books.google.com
}}
</ref>
The array's name and index number are a synonym for the data's ___location in memory.<ref>
{{Cite webbook
| access-date = 25 August 2025
| author = Graham M. Seed
Line 383 ⟶ 386:
| quote = The name of an array is a synonym for the memory ___location of the array.
| title = An Introduction to Object-Oriented Programming in C++, 7.12.1 One-Dimensional Arrays
| url = https://books.google.com/books?id=_lqj98AsnGAC&q=zeroth+element&pg=PA210#v=snippet&q=zeroth%20element&f=false
| isbn = 978-1-85233-450-5
| website = books.google.com
}}
</ref>
Line 497 ⟶ 500:
}}</ref>
 
=== TokensSyntax <span class="anchor" id="Syntax"></span><span class="anchor" id="Tokens"></span><span class="anchor" id="Metacharacters"></span><span class="anchor" id="Operators"></span><span class="anchor" id="Words"></span><span class="anchor" id="Names"></span> ===
 
<!-- Important concepts:
: <syntaxhighlight lang = text>
[] stream of characters
Tokens
[] delineate full commandlines (newline, semi-colon)
\_ Blanks
[] division into commands and parts of commands (optargs)
\_ Operators (Ops)
[] uses metacharacters
\_ Control Ops
-->
\_ Redirection Ops
<syntaxhighlight lang = text>
\_ Words
"Tokens"
\_ Reserved Words
\_ "Blanks"
\_ Names
\_ "Operators" (Ops)
\_ "Control Ops"
\_ "Redirection Ops"
\_ "Words"
\_ "Reserved Words"
\_ "Names"
</syntaxhighlight>
 
{{Blockquote
When Bash reads a full commandline, the complete string is broken down according to a certain set of rules into individual units called ''tokens.''
| syn{{dot}}tax '''1 a :''' the way in which linguistic elements (such as words) are put together to form constituents (such as phrases or clauses) '''b :''' the part of grammar dealing with this<ref>
''Tokens'' are identified using, and separated from each other using ''metacharacters.'' (As of version 5.3:)
{{Cite web
| access-date = 25 August 2025
| title = syntax, noun
| url = https://www.merriam-webster.com/dictionary/syntax
| website = merriam-webster.com
}}
</ref>
| author = Merriam-Webster Dictionary
}}
 
 
When Bash reads a ''full commandline,'' the complete string is broken down according to a certain set of rules into individual units called ''"tokens."''
"Tokens" are identified using, and separated from each other using ''"metacharacters."'' (As of version 5.3:)
 
* The ten ''"metacharacters:"''
Line 517 ⟶ 539:
: {{Pre |‘space’ ‘tab’ ‘newline’ ‘|’ ‘&’ ‘;’ ‘(’ ‘)’ ‘<’ ‘>’}}
 
''"Blanks"'' are composed entirely of unquoted metacharacters, ''"operators"'' each contain at least one unquoted metacharacter and ''"words"'' may not include any unquoted metacharacters.
 
In practice, Bash breaks down ''full command strings'' into tokens or groups of tokens that ''do'' contain metacharacters as well asand tokens or groups of tokens that do ''not'' contain any metacharacters -- called ''"words."''
From there it further breaks ''words'' down into more specific, meaningful pieces like command names, variable assignment statements, etc.
 
Line 540 ⟶ 562:
</syntaxhighlight>
 
A ''"word"'' is a sequence of (non-meta-) characters treated as a single unit by the shell.
A ''"reserved word"'' is a kind of a ''"word"'' that has a special meaning to the shell.<ref>
{{Cite web
| access-date = 18 August 2025
Line 548 ⟶ 570:
}}
</ref>
A ''"name"'' is a kind of a ''"word,"'' separate from ''reserved words'', which consists solely of letters, underscores and numbers; which begins with either a letter or an underscore; which, however, may not begin with a number.
''"Names,"'' also called ''"identifiers,"'' may be used for naming variables and functions.
 
* ''"Words"''
Line 565 ⟶ 587:
</syntaxhighlight>
 
In thisthe following example of a full command string, ''metacharacters'' have an comma placed above them, {{Code| ,}}, ''reserved words'' have an carrat placed beneath them, {{Code| ^}}, and other ''tokens'' have a backtick placed also beneath them, {{Code| `}}.
 
: <syntaxhighlight lang = console>
Line 572 ⟶ 594:
$ # ^^ ```` ``` ^^^^ ``````` ^^
</syntaxhighlight>
 
=== Subshells <span class="anchor" id="Subshells"></span> ===
 
<!-- Important concepts:
[x] Subshells
[x] Process and environment info, ie, parent/child, exact copy, etc
-->
 
A "subshell" is an additional instance of the shell which has been intitialized by a current instance of the shell.
When a "parent" shell creates a subshell, or a "child" shell, an exact copy of the parent's environment information is re-created and becomes the environment of the subshell.
 
In Bash, in non-arithmetic contexts, one can force the use of a subshell by enclosing a full command string in ''single parentheses''.
 
: <syntaxhighlight lang = console>
$ echo foo
foo
$ ( echo foo )
foo
$
</syntaxhighlight>
 
For this simple case, the preceding two commands are equivalent, however, use of subshells can have certain unexpected side effects.
There are numerous different forms of syntax which can cause the initialization of a subshell.
 
=== Expansion <span class="anchor" id="Expansion"></span> ===
 
<!-- Important concepts:
[]
-->
"Expansion" is a crucial concept in Unix-like shells. See [[String interpolation]].
 
Line 1,114 ⟶ 1,162:
[ ] scripts execute the contents of a file in a subshell
-->
 
With the {{code| source}}, or synonymous {{code| .}} command, Bash reads and executes shell commands from a file by name.<ref name="gnuBuiltin">{{cite web |title=4.1 Bourne Shell Builtins |url=https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html |website=4 Shell Builtin Commands |publisher=[[Free Software Foundation, Inc.]] |access-date=August 25, 2025}}</ref>
With the {{code| source}}, or synonymous {{code| .}} command, Bash reads and executes shell commands from any text file by name.<ref>
{{Cite web
| access-date = 26 August 2025
| publisher = [[Free Software Foundation, Inc.]]
| title = 4.1 Bourne Shell Builtins
| url = https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html
| website = gnu.org
}}
</ref>
 
=== Login and non-login shells <span class="anchor" id="Login and non-login shells"></span><span class="anchor" id="Login shells"></span><span class="anchor" id="Non-login shells"></span> ===
Line 1,693 ⟶ 1,750:
| quote = 19 :: SIGSTOP :: Stop, usually Ctrl + z
| title = Mastering Unix Shell Scripting, 2e
| url = https://books.google.com/books?hl=en&lr=&id=uI7C2BDF6F0C
}}</ref>
When a process receives a SIGKILL, the process terminates immediately and messily.
Line 1,706 ⟶ 1,763:
| quote = Use KILL only as a last resort!
| title = Learning the bash Shell: Unix Shell Programming
| isbn = 978-0-596-55500-9
| url = https://books.google.com/books?hl=en&lr=&id=dzBCH3x6fYEC
| url = https://books.google.com/books?id=dzBCH3x6fYEC
}}</ref>
The SIGKILL signal cannot be blocked or handled.
Line 1,777 ⟶ 1,835:
| quote = In Korn shell the {{Mono |echo}} command recognizes these command options by default. In Bash shell we must add the {{Mono |-e}} switch to the {{Mono |echo}} command, {{Code| echo -e "\n"| bash}} for one new line.
| title = Mastering Unix Shell Scripting, 2e
| url = https://books.google.com/books?hl=en&lr=&id=uI7C2BDF6F0C
}}</ref>
The list of options is not uniform across implementations, though {{mono|echo}} and {{mono|printf}} are both specified by POSIX.
Line 2,099 ⟶ 2,157:
| publisher = [[GNU Project]]
| title = Bash Reference Manual: 4.3.1: The Set Builtin
| url = https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
/software/bash/manual/html_node/The-Set-Builtin.html
| website = [[Free Software Foundation, Inc.]]
}}</ref>
Line 2,278 ⟶ 2,335:
-->
{{Blockquote
| ITERATION: Sometimes programs are repeated indefinitely or until a specific outcome is reached. Each execution of the instructions is an “iteration"iteration."<ref>
{{Cite web
| access-date = 15 August 2025
Line 2,285 ⟶ 2,342:
| url = https://onlinegrad.syracuse.edu/blog/coding-terms-for-beginners/
| website = syracuse.edu
| date = 13 January 2020
}}</ref>
| author =
Line 2,417 ⟶ 2,475:
| quote = Learning this now can save us a lot of pain and heartache later, especially....
| title = Mastering Linux Shell Scripting
| isbn = 978-1-78439-759-3
| url = https://books.google.com/books?hl=en&lr=&id=ITjlCwAAQBAJ
| url = https://books.google.com/books?id=ITjlCwAAQBAJ
}}</ref>
| author = Mastering Linux Shell Scripting, by Andrew Mallett
Line 2,533 ⟶ 2,592:
 
=== Tilde Expansion ===
{{Empty section|date=August 2025}}
 
Tilde expansion {{Code| ~}},
 
=== Parameter and variable expansion ===
Line 2,921 ⟶ 2,979:
It is recommended to use {{mono|sudo}} on a per-command basis instead.
 
=== CGI Scriptsscripts ===
 
CGI scripts are a significant source of vulnerability.<ref>
Do not use Shell for CGI scripts. You have been warned.<ref>
{{Cite web
| access-date = 17 August 2025
Line 2,980 ⟶ 3,038:
| author = OWASP Input Validation Cheat Sheet
}}
 
=== Command injection ===
 
* CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')
| url = https://cwe.mitre.org/data/definitions/77.html
| website = mitre.org
}}</ref>
 
* CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
| url = https://cwe.mitre.org/data/definitions/78.html
| website = mitre.org
}}</ref>
 
* CWE-88: Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-88: Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')
| url = https://cwe.mitre.org/data/definitions/88.html
| website = mitre.org
}}</ref>
 
Clearlisting is more effective than blocklisting.
 
* CWE-184: Incomplete List of Disallowed Inputs<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-184: Incomplete List of Disallowed Inputs
| url = https://cwe.mitre.org/data/definitions/184.html
| website = mitre.org
}}</ref>
 
=== Path traversal ===
 
* CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
| url = https://cwe.mitre.org/data/definitions/22.html
| website = mitre.org
}}</ref>
 
=== TOCTOU errors (Race conditions) ===
 
* CWE-367: Time-of-check Time-of-use (TOCTOU) Race Condition<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-367: Time-of-check Time-of-use (TOCTOU) Race Condition
| url = https://cwe.mitre.org/data/definitions/367.html
| website = mitre.org
}}</ref>
 
The {{Code| mkdir| bash}} and {{Code| mktemp| bash}} commands are known to create directories / files in an [[ACID |atomic]] fashion.
 
=== Untrusted search PATH ===
 
* CWE-426: Untrusted Search Path<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-426: Untrusted Search Path
| url = https://cwe.mitre.org/data/definitions/426.html
| website = mitre.org
}}</ref>
 
* CWE-427: Uncontrolled Search Path Element<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-427: Uncontrolled Search Path Element
| url = https://cwe.mitre.org/data/definitions/427.html
| website = mitre.org
}}</ref>
 
* CWE-428: Unquoted Search Path or Element<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-428: Unquoted Search Path or Element
| url = https://cwe.mitre.org/data/definitions/427.html
| website = mitre.org
}}</ref>
 
=== Abuse of symlinks ===
 
* CWE-61: UNIX Symbolic Link (Symlink) Following<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-61: UNIX Symbolic Link (Symlink) Following
| url = https://cwe.mitre.org/data/definitions/61.html
| website = mitre.org
}}</ref>
 
=== Sensitive information in error messages ===
 
* CWE-535: Exposure of Information Through Shell Error Message<ref>
{{Cite web
| access-date = 17 August 2025
| publisher = [[Mitre Corporation |The MITRE Corporation]]
| title = CWE-535: Exposure of Information Through Shell Error Message
| url = https://cwe.mitre.org/data/definitions/535.html
| website = mitre.org
}}</ref>
 
=== Shellshock ===
Line 3,194 ⟶ 3,137:
 
== Deprecated syntax <span class="anchor" id="Deprecated syntax"></span> ==
{{Unreferenced section|date=August 2025}}
 
* [[Backtick]] style command substitutions: {{Code| `...`| bash}} is deprecated in favor of
Line 3,855 ⟶ 3,799:
 
=== Timeline ===
<!-- consider moving to dedicated 'Timeline of Bash' article -->
Significant events in Bash history are listed below:
 
{| class = "wikitable sortable collapsible collapsed"
! Date
! Event
Line 4,153 ⟶ 4,099:
Bash became the default shell on Apple's operating systems (i.e., MacOS) starting with OS X 10.3 Panther.<ref>[https://www.google.com/books/edition/Essential_Mac_OS_X_Panther_Server_Admini/zrI-U0KWj3cC?hl=en&gbpv=1&dq=bash&pg=PA189&printsec=frontcover Essential Mac OS S Panther Server Administration, pg 189]
</ref><ref>
{{Cite webbook
| access-date = 8 August 2025
| archive-date = 2 March 2021
Line 4,171 ⟶ 4,117:
| url = https://books.google.com/books?id=dwIRERUpQPEC&q=bash+most+popular+unix+shell&pg=PA6
| url-status = live
| website = google.com
}}
</ref>
Line 4,447 ⟶ 4,392:
 
=== Unix shells ===
{{div col|colwidth=24em}}
 
* [[Almquist shell | Almquist shell (ash)]]
* [[Bourne shell | Bourne shell (sh)]]
Line 4,477 ⟶ 4,422:
* yash – Yet Another Shell, aims "to be the most POSIX-compliant shell in the world"; available on Arch.
* [[Z shell | Z shell (zsh)]]
{{Cleardiv col end}}
 
<!-- This subsection added from https://ru.wikipedia.org/wiki/Bash on 6 Aug 2025 -->
 
=== Graphical interface to scripts ===
There are many programs that allow you to create a graphical interface for shell scripts.