Bash (Unix shell): Difference between revisions

Content deleted Content added
Undid revision 1307857444 by OceanLoop Reverting disruptive edit from yesterday. Work on the prose is in-process.
Tags: Undo Reverted
Undid revision 1307855327 by OceanLoop Reverting second disruptive edit.
Tags: Undo Reverted
Line 241:
=== 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 249:
[ ] 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 321 ⟶ 329:
</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 342 ⟶ 346:
</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 407:
 
=== 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 416 ⟶ 422:
}}
 
<!-- 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 529 ⟶ 530:
[] 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 555 ⟶ 548:
}}
 
 
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 645 ⟶ 669:
 
<!-- Important concepts:
[] replacement of placeholders / logical substitution
[]
[] Expansion vs substitution
-->
 
"Expansion" is a crucial concept in Unix-like shells. See [[String interpolation]].
 
Line 677 ⟶ 703:
[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 816 ⟶ 843:
 
=== Processes <span class="anchor" id="Processes"></span> ===
 
{{Main
| Process (computing)
| Attribute (computing)
}}
 
<!-- Important concepts:
Line 830 ⟶ 852:
[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 871 ⟶ 899:
 
=== 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 886 ⟶ 908:
[?] 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 928 ⟶ 957:
 
=== Paths <span class="anchor" id="Paths"></span> ===
 
{{Main |Path (computing)
| Hierarchical file system
}}
 
<!-- Important concepts:
Line 938 ⟶ 963:
[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 963 ⟶ 993:
 
=== 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.
 
{{Main
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.
| Execution (computing)
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.
 
"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.
:<syntaxhighlight lang="console">
$ 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,470 ⟶ 1,483:
[x] Description and visual example of xtrace
[x] How to enable and disable xtrace
[x] Thompson-style comments visible in xtrace
[x] xtrace re POSIX
-->
Line 1,489 ⟶ 1,503:
$
</syntaxhighlight>
 
The {{Mono| xtrace}} shell setting is specified by POSIX.
See also {{section link||Debugging}}.
 
=== The {{mono| verbose}} option ===
 
<!-- Important concepts:
[x] Saying something about Verbose mode
-->
The verbose option prints strings to the terminal as they are read, and before any expansions are performed. Rarely used.<ref>See {{Code| set -v| bash}} in the documentation.</ref>
 
=== Comments ===
 
<!-- Important concepts:
[x] Comments
[x] Inline comments
[x] Observability of comments
-->
Comments can be a valuable way of clarifying information or explaining a script or source file to someone else who might not be familiar with the scripter's intentions or context.
 
Standard comments in Bash are denoted with a hashtag character: {{char|#}}.
Any text to the right of the hashtag to the end of the line will be ignored.
Inline comments are allowed, but hashtag comments will not print during debugging. See also: {{section link||xtrace}}.
 
Comments denoted with a colon character, {{char|:}}, originated with the [[Thompson shell]].<ref>
Line 1,534 ⟶ 1,525:
 
:<syntaxhighlight lang="console">
$ # Define foo=bar
$ foo=bar # An inline hashtag comment occurs on the same line as a command
$ set -x
$ # A regular comment (no output)
$ : "${foo}"
+ : bar
$
</syntaxhighlight>
 
The {{Mono| xtrace}} shell setting is specified by POSIX.
See also {{section link||Debugging}}.
 
=== The {{mono| verbose}} option ===
 
<!-- Important concepts:
[x] Saying something about Verbose mode
-->
 
The verbose option prints strings to the terminal as they are read, and before any expansions are performed. Rarely used.<ref>See {{Code| set -v| bash}} in the documentation.</ref>
 
=== Exit codes ===
Line 1,553:
[x] Lack of standardization
-->
 
When bash executes commands, [[exit status]] codes, also called "return codes," are produced which can offer some insight into the manner in which a program ceased running.
The value of the most recently captured exit code is held within the shell parameter, 'question mark:' {{Code| $? }}.