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
linux:bash [2026/01/11 11:57] – [Control Operators] 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 157: Line 157:
  
 Because the file did not exist, the ''echo'' command was not run. Because the file did not exist, the ''echo'' command was not run.
 +
 ===== Arguments ===== ===== Arguments =====
  
Line 162: 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 Multiple arguments can be used, separated by a blank space
Line 173: 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 192: 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 253: 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 265: 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 275: 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 311: 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 320: 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 338: 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 400: 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 407: 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 413: 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 441: 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.txt
  • Last modified: 2026/10/31 22:42
  • by claude