@@ -17,6 +17,7 @@ Bash is a Turing-complete language and provides a large variety of features.
1717---
1818
1919Some of them are common to most popular programming languages:
20+
2021- variables
2122- conditional statements
2223- loops
@@ -27,6 +28,7 @@ Some of them are common to most popular programming languages:
2728---
2829
2930Others are specific to bash and the terminal environment:
31+
3032- pipes
3133- job controls
3234- shell expansions
@@ -53,14 +55,17 @@ Bash can be used either:
5355## Interactive mode
5456
5557This is an example of a minimal prompt.
58+
5659``` bash
57- $
60+ $
5861```
62+
5963It signifies that the shell is ready to accept commands.
6064
6165<bonus-content >
6266
6367The prompt can be customized to include any desirable information such as:
68+
6469- the current directory
6570- the machine name
6671- the current user
@@ -81,38 +86,58 @@ The prompt can be customized to include any desirable information such as:
8186
8287echo ' Hello World!'
8388```
89+
8490Once the user submits the command, it executes, and prints the output to the screen.
8591
86- The ` echo ` command prints the provided argument to ** standard out **
92+ The ` echo ` command prints the provided argument to __ standard out __
8793
8894---
8995
9096> * Beware* : Bash is case-sensitive, so trying
97+ >
9198> ``` bash
9299> EcHo ' hello world'
93100> ` ` `
101+ >
94102> might NOT produce the expected result
95103
96104---
97105
98106# # Anatomy of a command
99107
100108All commands in bash follow the following general structure:
109+
101110` ` ` bash
102111[variables] [command] [options] [arguments] [redirects] [operators]
103112```
113+
104114The command is the only mandatory element, everything else is optional
105115
116+ <bonus-content >
117+
118+ ## Command parsing order
119+
120+ Bash processes commands in this order:
121+
122+ 1 . History expansion (if enabled)
123+ 2 . Quote removal and word splitting
124+ 3 . Expansions (brace, tilde, parameter, arithmetic, command substitution, pathname)
125+ 4 . Redirection setup
126+ 5 . Command execution
127+
128+ </bonus-content >
129+
106130---
107131
108132## Output
109133
110134There are 2 main output streams:
111- - standard output (** stdout** ) for regular program output
112135
113- - standard error (** stderr** ) for error messages and diagnostics.
136+ - standard output (__ stdout__ ) for regular program output
137+
138+ - standard error (__ stderr__ ) for error messages and diagnostics.
114139
115- > ** By default, both streams are displayed on the terminal.**
140+ > __ By default, both streams are displayed on the terminal.__
116141
117142---
118143
@@ -121,6 +146,7 @@ There are 2 main output streams:
121146There are several types of commands in bash.
122147
123148When trying to execute a command bash will search for it in this order:
149+
124150- aliases
125151- keywords
126152- functions
@@ -130,6 +156,7 @@ When trying to execute a command bash will search for it in this order:
130156---
131157
132158Use the ` type ` built-in command to check the type of another command.
159+
133160``` bash
134161type grep
135162# prints: grep is /usr/bin/grep
@@ -149,25 +176,26 @@ type ll
149176# ## Built-ins and keywords
150177
151178Use the ` help` command to get basic information about built-ins and keywords.
179+
152180` ` ` bash
153181help # prints all built-ins and keywords
154182
155183help if # prints information about the if keyword
156184` ` `
157185
158-
159186---
160187
161188# ## Aliases
162189
163190Use the ` alias` command to view or define aliases.
191+
164192` ` ` bash
165193alias # prints all aliases
166194
167195alias ll=' ls -alh' # define a new alias
168196` ` `
169197
170- > ** NOTE ** : there must be NO spaces around the ` =` sign.
198+ > __NOTE__ : there must be NO spaces around the ` =` sign.
171199> Also, unlike other languages, bash handles single and double quotes differently.
172200
173201---
@@ -223,6 +251,7 @@ Fortunately, bash provides a special variable `$?` to indicate the exit status o
223251---
224252
225253A value of ` 0` means success. Any other (positive number) indicates an error.
254+
226255` ` ` bash
227256type ls
228257echo $?
@@ -239,6 +268,7 @@ echo $?
239268< pop-quiz data-answer-id=" 2" >
240269
241270# ## Which is *NOT* a valid command type?
271+
242272- built-in
243273- file (external)
244274- interactive
@@ -264,10 +294,12 @@ echo $?
264294Use the ` env` command to see all variables defined on the system.
265295
266296Or ` echo` to print the value of a specific variable:
297+
267298` ` ` bash
268299echo $BASH_VERSION
269300# prints: bash-5.3
270301` ` `
302+
271303> NOTICE the ` $` character used to reference the variables
272304
273305---
@@ -282,6 +314,7 @@ MY_VAR='hello' fruit='banana'
282314echo $fruit $MY_VAR
283315# prints: banana hello
284316` ` `
317+
285318> NOTICE there is NO ` $` character when defining variables.
286319
287320< bonus-content>
@@ -332,7 +365,7 @@ echo $TZ
332365
333366---
334367
335- The ` $` character is used for ** variable expansion ** .
368+ The ` $` character is used for __variable expansion__ .
336369
337370It allows multiple advanced features:
338371
@@ -356,6 +389,7 @@ echo ${my_var:1:3}
356389# # Single vs double quotes
357390
358391Single quotes denote a literal string, while double quotes allow for variable expansion.
392+
359393` ` ` bash
360394echo ' $MY_VAR test'
361395# prints: $MY_VAR test
@@ -374,6 +408,7 @@ echo "$MY_VAR test"
374408
375409By default, the output of a command goes to stdout. But there are ways to redirect or capture
376410the result of a command:
411+
377412` ` ` bash
378413today=date
379414echo $today
@@ -387,9 +422,10 @@ echo $today
387422---
388423
389424Subshells can also be used to pass the output of one command as an argument to another:
425+
390426` ` ` bash
391427du -h $( which ls)
392- # print: 40K /bin/ls
428+ # print: 40K /bin/ls
393429` ` `
394430
395431- ` du` returns the size of a file
@@ -406,7 +442,7 @@ env | grep MY_
406442# prints all global variables that contain the string MY_
407443` ` `
408444
409- Using a pipe we are redirecting the output of the first command to the ** stdin ** of the second.
445+ Using a pipe we are redirecting the output of the first command to the __stdin__ of the second.
410446
411447< bonus-content>
412448
@@ -418,9 +454,8 @@ Use bash to customize your prompt.
418454Explore options for what information would be most useful to you.
419455Play around with fun colors and special symbols.
420456
421- _ ** Hint ** : to modify your prompt set a value to the ` PS1` special variable._
457+ * __Hint__ : to modify your prompt set a value to the ` PS1` special variable.*
422458
423459< /home-work>
424460
425461< /bonus-content>
426-
0 commit comments