linux:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:bash [2022/02/11 11:02] – [Generalities] mhlinux:bash [2026/10/31 22:42] (current) – [claude] fix command expansion, tilde and echo examples, heading levels, https link, typos claude
Line 11: Line 11:
 Bash can be used **interactively** in the terminal emulator or **non-interactively** as a written script of commands that is executed. Bash can be used **interactively** in the terminal emulator or **non-interactively** as a written script of commands that is executed.
  
-When bash starts a program, the system creates a running **process** for it. Bash creates **file descriptors**  and connects them to the same **streams** as it's own : (keyboard to FD:0 (input) and FD:1 (output) and FD:2 (error) to display 0). The pipe | is vastly used syntax sugar to connect the output of a command to the input of another.+When bash starts a program, the system creates a running **process** for it. Bash creates **file descriptors**  and connects them to the same **streams** as its own: keyboard → FD 0 (input), display ← FD 1 (output) and FD 2 (error). The pipe | is vastly used syntax sugar to connect the output of a command to the input of another.
  
 Bash has **synchronous command execution** meaning it will execute commands one at a time and users cannot interact with bash while it is executing a command. Bash has **synchronous command execution** meaning it will execute commands one at a time and users cannot interact with bash while it is executing a command.
Line 19: Line 19:
 Bash is **not a strict interpreter**.  Bash is **not a strict interpreter**. 
  
-Beware of the ambiguity we are used to while expressing ourselves with human languages.+**Beware of the ambiguity** we are used to while expressing ourselves with human languages.
  
-The discipline of writing clear, safe and precise code lies heavily upon the user. +The **discipline of writing clear, safe and precise code** lies heavily upon the user. 
  
-**Don't write bad bash code.**+__**Don't write bad bash code.**__
 </WRAP> </WRAP>
  
Line 35: Line 35:
 Many command types are syntax sugar: their effect can be achieved differently, but they exist to make the job easier.  Many command types are syntax sugar: their effect can be achieved differently, but they exist to make the job easier. 
 </WRAP> </WRAP>
 +
 +==== Variables ====
 +
 +Before a command's name you can insert one or many ''var=value'' assignments. These variables will apply only to that one command.
  
 ==== Basic commands ==== ==== Basic commands ====
Line 41: Line 45:
  
 Bash performs a search using this name and looks for :  Bash performs a search using this name and looks for : 
-  *alias (first)+  *alias (before anything else)
   *functions   *functions
   *built-in commands   *built-in commands
Line 59: Line 63:
 $ echo $name $ echo $name
 john john
 +</code>
 +----
 +
 +=== type ===
 +
 +Displays information on a command, and where it is stored.
 +
 +//Note that the **type** command and the **which** program are different and give different outputs//
 +
 +**Example**
 +<code bash>
 +$ type ls
 +ls is aliased to 'ls -al'
 </code> </code>
 ---- ----
Line 95: Line 112:
 </code> </code>
 ---- ----
 +
 +=== 2>/dev/null || true ===
 +
 +<code bash>
 +$ something 2>/dev/null || true
 +</code>
 +
 +This means:
 +
 +Try something, but if it errors (exit code ≠ 0): 
 +  * silence the error message (send to ''/dev/null'')
 +  * immediately run ''true'' ( || operator passes along to second part)
 +  * Overall ''exit'' status becomes success (exit code = 0)
 +
 +In plain English:
 +
 +“Try to do the cleanup.
 +If there’s nothing to clean, that’s fine. Move on.”
 +
 +This pattern is **not appropriate** when:
 +  * Failure is meaningful
 +  * You want to be alerted
 +  * Data loss is possible
 +  * You are debugging
 +
 +In those cases, we want ''stderr'' and non-zero exits.
 +
 +So this is not about hiding problems.
 +It’s about acknowledging expected non-problems.
 +
 +----
 +
 +=== && AND Operator ===
 +
 +This control operator tells bash to run a second command **only if the first command before it succeeded**.
 +
 +If the first command fails, the second command is entirely skipped.
 +
 +**Example**
 +<code bash>
 +$ rm hello.txt && echo "Deleted file."
 +rm: cannot remove 'hello.txt': No such file or directory
 +</code>
 +
 +Because the file did not exist, the ''echo'' command was not run.
  
 ===== Arguments ===== ===== Arguments =====
Line 101: Line 163:
 **Arguments are a sequence of characters considered as a single unit by the shell.** **Arguments are a sequence of characters considered as a single unit by the shell.**
  
-They can be a filename, a variable name, the name of a program or just a litteral.+They can be a filename, a variable name, the name of a program or just a literal. 
 + 
 +Multiple arguments can be used, separated by a blank space
 </WRAP> </WRAP>
  
Line 110: Line 174:
 Escaping is the act of adding a single \ before the character we want to escape. Escaping is the act of adding a single \ before the character we want to escape.
  
-Quoting is a better practice than escaping and should be use more often than not. Quoting is sometimes unnecessary but it is rarely wrong to quote.+Quoting is a better practice than escaping and should be used more often than not. Quoting is sometimes unnecessary but it is rarely wrong to quote.
  
 <WRAP round important> <WRAP round important>
Line 129: Line 193:
 $ read -p 'Which user would you like to remove from your system? ' username $ read -p 'Which user would you like to remove from your system? ' username
 Which user would you like to remove from your system?  joe  Which user would you like to remove from your system?  joe 
-#Note that the user accidentaly inputed a whitespace before the word : his input is ' joe'+#Note that the user accidentally entered a whitespace before the word : his input is ' joe'
  
 $ rm -vr /home/$username $ rm -vr /home/$username
Line 184: Line 248:
 There can be **no whitespace** around the = operator !  There can be **no whitespace** around the = operator ! 
  
-If a whitespace is required in the variable, use '' quotes to create a literal+If a whitespace is required in the variable, use 'single quotes' to create a literal
 </WRAP> </WRAP>
  
Line 190: Line 254:
  
 <code bash> <code bash>
-$ contents="cat hello.txt" +$ contents="$(cat hello.txt)" 
 # Don't forget to "double quote" ! # Don't forget to "double quote" !
 </code> </code>
Line 202: Line 266:
 <code bash> <code bash>
 $ name=mh time=8 $ name=mh time=8
-$ echo "$name, it is ${time}h;"+$ echo "$name, it is ${time}h."
 mh, it is 8h. mh, it is 8h.
 </code> </code>
Line 212: Line 276:
  
 === WHITESPACE === === WHITESPACE ===
-Whitespace in bash have a special meaning : they split commands into arguments.+Whitespace in bash has a special meaning : they split commands into arguments.
  
 === * === === * ===
-A star or asterix matches any kind of text, even no text at all.+A star or asterisk matches any kind of text, even no text at all.
 ---- ----
  
Line 248: Line 312:
 ==== Tilde expansion ==== ==== Tilde expansion ====
  
-**Tilde expansion** happens earlier in the parser phrase, and is different from //pathname expansion//.+**Tilde expansion** happens earlier in the parser phase, and is different from //pathname expansion//.
  
 It is always replaced by an explicit pathname pointing to the home directory of the user or of another user explicitly named after the tilde. It is always replaced by an explicit pathname pointing to the home directory of the user or of another user explicitly named after the tilde.
Line 257: Line 321:
  
 $ echo ~root $ echo ~root
-/var/root+/root
 </code> </code>
  
-===== Value Expansions ====+===== Value Expansions =====
  
 **Command expansions** are a very useful value expansion example. You can call a command within a command by using $(...) syntax. **Command expansions** are a very useful value expansion example. You can call a command within a command by using $(...) syntax.
Line 275: Line 339:
 You must **always** use "double-quotes" when using value expansions or the $ sign will be output as a literal between 'single quotes' You must **always** use "double-quotes" when using value expansions or the $ sign will be output as a literal between 'single quotes'
 </WRAP> </WRAP>
 +
 ===== Redirection ===== ===== Redirection =====
  
Line 324: Line 389:
 As a result, both file descriptors are connected to the same stream. As a result, both file descriptors are connected to the same stream.
  
 +Be careful not to write something like ''$ ls -l a b >myfiles.ls 2>myfiles.ls'' as due to the way streams are handled they will most likely end up mixing up and garbling the file 'myfiles.ls'
 </WRAP> </WRAP>
  
Line 336: Line 402:
 <code bash> <code bash>
 #This convenience sugar syntax exists for directing both FD1 & FD2 to the same location #This convenience sugar syntax exists for directing both FD1 & FD2 to the same location
-$ping 127.0.0.1 &>results+$ ping 127.0.0.1 &>results
 </code> </code>
  
Line 343: Line 409:
 ==== Append to a file ==== ==== Append to a file ====
  
-Use double brackets :+Use double greater-than :
 <code bash> <code bash>
 echo Hello >~/myfile echo Hello >~/myfile
Line 349: Line 415:
 </code> </code>
 ---- ----
 +
 ===== Bash Scripting ===== ===== Bash Scripting =====
  
-=== Hashbangs ===+==== Hashbangs ====
  
 To make a script out of a file containing bash commands, add a hashbang at the beginning of the file : To make a script out of a file containing bash commands, add a hashbang at the beginning of the file :
Line 377: Line 444:
 Command names with a slash are always considered direct pathnames to the program to execute. Command names with a slash are always considered direct pathnames to the program to execute.
 </WRAP> </WRAP>
-===== Ressources =====+ 
 +===== Resources =====
  
   *[[https://guide.bash.academy/|Bash Academy]]   *[[https://guide.bash.academy/|Bash Academy]]
   *[[https://ss64.com/bash/|A-Z index of Bash commands]]   *[[https://ss64.com/bash/|A-Z index of Bash commands]]
   *[[https://www.cyberciti.biz/faq/bash-infinite-loop/|Infinite loop examples]]   *[[https://www.cyberciti.biz/faq/bash-infinite-loop/|Infinite loop examples]]
-  *[[http://mywiki.wooledge.org/EnglishFrontPage|Greg's Wiki Front Page]]+  *[[https://mywiki.wooledge.org/EnglishFrontPage|Greg's Wiki Front Page]]
  • linux/bash.1644573735.txt.gz
  • Last modified: 2025/06/02 21:18
  • (external edit)