Bash (Unix shell): Difference between revisions

Content deleted Content added
Definitions: Move the paragraph on delineating lines from character streams down from subsection Execution to subsection Syntax
Tag: Reverted
m fix quot; repair
 
(23 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 237 ⟶ 239:
=== ASCII, strings and numbers <span class="anchor" id="ASCII"></span> <span class="anchor" id="strings"></span> <span class="anchor" id="numbers"></span> ===
 
<!-- Important concepts: ASCII
[x] Characters
[x] Human/computer communitcation
Line 245 ⟶ 247:
[ ] Information
-->
<!-- Important concepts: strings
[x] String
[x] Filenames
-->
<!-- Important concepts: numbers
[x] Zero-based numbering
-->
 
{{Blockquote
| The input language to the shell shall be first recognized at the character level.<ref>
Line 273 ⟶ 267:
| url = https://developer.mozilla.org/en-US/docs/Glossary/Character
| website = mozilla.org
| date = 11 July 2025
}}
</ref>
Line 324 ⟶ 319:
</syntaxhighlight>
 
<!-- Important concepts:
[x] String
[x] Filenames
-->
Any series of characters is called a "[[String (computer science) |string]]," or sometimes a "[[string literal]]."
In Unix-like operating systems, all characters, printable and non-printing, except for a few such as the [[null character]] and forward slash {{char|/}}, can be used in naming [[Filenames |files]].
Line 337 ⟶ 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>
 
<!-- Important concepts:
[x] Zero-based numbering
[]
-->
In everyday life, people usually begin counting some group of items from the number one: 1, 2, 3 apples.
In computer science it is customary to do as the computers do and begin counting the first item from the number zero: 0, 1, 2 oranges, for a total of 3 oranges.
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 402 ⟶ 405:
 
=== CLI and GUI <span class="anchor" id="CLI and GUI"></span> ===
 
<!-- Important concepts:
[x] Origin of CLI paradigm
[x] Origin of terminal emulators
[x] GUI
-->
 
{{Main
Line 417 ⟶ 414:
}}
 
<!-- Important concepts:
[x] Origin of CLI paradigm
[x] Origin of terminal emulators
[x] GUI
-->
Long after the first Analytical Engine, in the mid-1960's one of the primary ways for humans and computers to interact in real time was at a keyboard with a [[teleprinter]]: only display of textual characters was possible.<ref>
{{Cite web
Line 503 ⟶ 505:
[] stream of characters
[] delineate full commandlines (newline, semi-colon)
[x] Bash reads one line at a time
[x] Line continuation
[] division into commands and parts of commands (optargs)
[] uses metacharacters
-->
<syntaxhighlight lang = text>
"Tokens"
\_ "Blanks"
\_ "Operators" (Ops)
\_ "Control Ops"
\_ "Redirection Ops"
\_ "Words"
\_ "Reserved Words"
\_ "Names"
</syntaxhighlight>
 
{{Blockquote
Line 521 ⟶ 531:
}}
 
 
A person typing at a keyboard creates something called a ''stream'' of characters.
In the [[Command-line interface#Comparison to graphical user interfaces |command-line interface]] [[paradigm]], via a [[terminal emulator]] Bash receives a series of characters from the user and expects each series of characters to be a command.
 
By default, Bash reads user code one line at a time, interprets any newline or semi-colon character {{code|;}} as the end of the current command, and executes commands in sequence.
If an interactive command extends beyond the width of the terminal emulator, it's usually possible to keep typing and the command will wrap around.
To extend a command beyond a newline onto an additional line, it's necessary that the final character of the first line be an unescaped backslash, {{code|\}}, which signals "line continuation."
Bash always finishes parsing and executing one full commandline before moving on to and beginning with the parsing of the next commandline.
 
:<syntaxhighlight lang="console">
$ foo=aa bar=bb quux=cc zork=dd; set -o xtrace
$ : "${foo}"; : "${bar}"
+ : aa
+ : bb
$ : "${quux}" \
> : "${zork}"
+ : cc : dd
$
</syntaxhighlight>
 
When Bash reads a ''full commandline,'' the complete string is broken down according to a certain set of rules into individual units called ''"tokens."''
 
<syntaxhighlight lang = text>
"Tokens"
\_ "Blanks"
\_ "Operators" (Ops)
\_ "Control Ops"
\_ "Redirection Ops"
\_ "Words"
\_ "Reserved Words"
\_ "Names"
</syntaxhighlight>
 
"Tokens" are identified using, and separated from each other using ''"metacharacters."'' (As of version 5.3:)
 
Line 642 ⟶ 621:
 
<!-- Important concepts:
[]
[] replacement of placeholders / logical substitution
[] Expansion vs substitution
-->
 
"Expansion" is a crucial concept in Unix-like shells. See [[String interpolation]].
 
Line 676 ⟶ 653:
[x] Arrays
-->
 
For data structures Bash offers variables and arrays, and though there are numerous kinds of each of these available, the data structures are relatively simple compared to other languages like [[C (programming language) |C]] or [[Java (programming language) |Java]].<ref>{{Cite web
| access-date = 15 August 2025
Line 817 ⟶ 793:
 
=== Processes <span class="anchor" id="Processes"></span> ===
 
{{Main
| Process (computing)
| Attribute (computing)
}}
 
<!-- Important concepts:
Line 826 ⟶ 807:
[x] Working directory
-->
 
{{Main
| Process (computing)
| Attribute (computing)
}}
 
Each [[operating system]] (OS) has at its core a program called the [[Kernel (operating system) |kernel]] which runs commands.<ref name = Brit_control-unit>
{{Cite web
Line 873 ⟶ 848:
 
=== Files and permissions <span class="anchor" id="Files and permissions"></span><span class="anchor" id="Files"></span><span class="anchor" id="Permissions"></span> ===
 
{{Main
| File system
| Unix file types
| File-system permissions
}}
 
<!-- This subsection is messy -->
Line 882 ⟶ 863:
[?] umask
-->
 
{{Main
| File system
| Unix file types
| File-system permissions
}}
 
In Unix-like operating systems, all objects locatable on the [[file system]] are considered to be "files."<ref name = tldp_3.1.1>
{{Cite web
Line 931 ⟶ 905:
 
=== Paths <span class="anchor" id="Paths"></span> ===
 
{{Main |Path (computing)
| Hierarchical file system
}}
 
<!-- Important concepts:
Line 937 ⟶ 915:
[x] File anchor: './'
-->
 
{{Main |Path (computing)
| Hierarchical file system
}}
 
In Unix-like OS's, files, hardlinks, device nodes, etc., (i.e., "files") are sorted into [[Directory (computing) |directories]] that form a hierarchical file structure which is nested in a "parent" and "child" manner.
The base of the hierarchy is called the "root directory" which is denoted by one forward slash: {{char|/}}.
Line 967 ⟶ 940:
 
=== Execution <span class="anchor" id="Execution"></span> ===
 
{{Main
| Execution (computing)
}}
 
<!-- Important concepts:
[x] Execution / to execute a command
[x] An executable
[x] Bash reads one line at a time
[x] Line continuation
[x] Command position
[ ] A single logical construct is parsed as a single unit
-->
"Execution" of a given program occurs when a user (or some other program) asks the operating system to act upon the instructions contained in the given program.
 
By default, Bash reads user code one line at a time, interprets any newline or semi-colon character {{code|;}} as the end of the current command, and executes commands in sequence.
{{Main
If an interactive command extends beyond the width of the terminal emulator, it's usually possible to keep typing and the command will wrap around.
| Execution (computing)
To extend a command beyond a newline onto an additional line, it's necessary that the final character of the first line be an unescaped backslash, {{code|\}}, which signals "line continuation."
}}
Bash always finishes parsing and executing one full commandline before moving on to and beginning with the parsing of the next commandline.
 
:<syntaxhighlight lang="console">
"Execution" of a given program occurs when a user (or some other program) asks the operating system to act upon the instructions contained in the given program.
$ foo=aa bar=bb quux=cc zork=dd; set -o xtrace
$ : "${foo}"; : "${bar}"
+ : aa
+ : bb
$ : "${quux}" \
> : "${zork}"
+ : cc : dd
$
</syntaxhighlight>
 
The first word of a commandline is known as the "command position."
Line 1,172 ⟶ 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,751 ⟶ 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,764 ⟶ 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,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,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,336 ⟶ 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,343 ⟶ 2,342:
| url = https://onlinegrad.syracuse.edu/blog/coding-terms-for-beginners/
| website = syracuse.edu
| date = 13 January 2020
}}</ref>
| author =
Line 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 4,098 ⟶ 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,116 ⟶ 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>