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 [2021/04/03 19:25] mhlinux:bash [2022/02/28 14:54] (current) mh
Line 3: Line 3:
 ===== Generalities ===== ===== Generalities =====
  
-Most of this comes from the excellent guide to bash : [[https://guide.bash.academy|Bash Academy]]+Most of this comes from the greatest guide to bash : [[https://guide.bash.academy|Bash Academy]]
  
-Bash is a **shell program** written to listen for **commands** from users and execute them. There are other shell programs (C shell, Z shell, Korn shell, etc.).+Bash is a **shell program** written to listen for **commands** from users and execute them. There are other shell programs (C shell, Z shell, Korn shell, etc.) so it is important to know for what shell you are writing code for.
  
 Bash uses **text-based interface** to interact with the users. It takes input (Std In) as text and displays output (Std Out) and error (Std Err) as text using a **terminal emulator** program. Bash uses **text-based interface** to interact with the users. It takes input (Std In) as text and displays output (Std Out) and error (Std Err) as text using a **terminal emulator** program.
Line 15: Line 15:
 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.
  
-Bash is **not a strict interpreter**. The discipline of writing clear, safe and precise code lies heavily upon the user.  
  
-**Don't write bad bash code.**+<WRAP round important> 
 +Bash is **not a strict interpreter**.  
 + 
 +**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.  
 + 
 +__**Don't write bad bash code.**__ 
 +</WRAP> 
 ===== Commands ===== ===== Commands =====
  
 <WRAP round info> <WRAP round info>
-Bash commands tell bash to perform a certain unit of work. These units of work cannot be subdivided: bash needs to know the whole command to be able to execute it. +Bash commands tell bash to perform a certain unit of work. These units of work cannot be subdivided: bash needs to know the whole command to be able to execute it. Also, bash reads commands line-by-line.
  
 There are different kinds of commands for different types of operations. Some commands group other commands into blocks or test their result.  There are different kinds of commands for different types of operations. Some commands group other commands into blocks or test their result. 
Line 27: 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 33: 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 51: 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 94: Line 119:
  
 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 litteral.
 +
 +Multiple arguments can be used, separated by a blank space
 </WRAP> </WRAP>
  
Line 176: Line 203:
 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 quotesto create a literal
 </WRAP> </WRAP>
  
Line 186: Line 213:
 </code> </code>
  
-To re-use a variable we use parameter expansion, prefixing the name of our variable with a $ sign. Whenever $ is prefixed, something is probably being expanded (a parameter, the output of a command, or the result of an arithmetic operation)+To re-use a variable we use parameter expansion, prefixing the name of our variable with a \$ sign. Whenever \$ is prefixed, something is probably being expanded (a parameter, the output of a command, or the result of an arithmetic operation)
  
 RULE : Whenever you expand : you **must** "double quote" ! RULE : Whenever you expand : you **must** "double quote" !
Line 202: Line 229:
  
 ==== Globs/Meanings ==== ==== Globs/Meanings ====
 +
 +=== WHITESPACE ===
 +Whitespace in bash have 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 asterix matches any kind of text, even no text at all.
Line 312: Line 343:
 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 343: Line 375:
 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 :
  
-This is a direct path to bash in many GNU/Linux distributions+This is a direct path to bash in many GNU/Linux distributions :
 <code bash> <code bash>
 #!/bin/bash  #!/bin/bash 
 </code> </code>
  
-This invokes the 'env' program giving bash as an argument explicitly asking 'env' to find the path to bash and return it. It is safer and more inclusive for covering more exotic distributions and other operating systems.+This rather more precise alternative invokes the 'env' program giving bash as an argument explicitly asking 'env' to find the path to bash and return it. It is safer and more inclusive for covering more exotic distributions and other operating systems :
 <code bash> <code bash>
 #!/usr/bin/env bash #!/usr/bin/env bash
  • linux/bash.1617470756.txt.gz
  • Last modified: 2021/04/03 19:25
  • by mh