Site Tools


Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
bash:cheat [2025-01-22] dcaibash:cheat [2025-03-21] (current) dcai
Line 1: Line 1:
 ===== bash cheat sheet ===== ===== bash cheat sheet =====
  
-=== IO Redirect ===+=== 💻 IO Redirect ===
  
-  * ''%%&1%%'' is stdout +  * ''%%command &>>/dev/null%%'': Redirect stdout and stderr to /dev/null 
-  * ''%%&2%%'' is stderr+  * ''%%&1%%'' is referenced to stdout 
 +  * ''%%&2%%'' is referenced to stderr
   * ''%%command 1>>/dev/null%%'': stdout go to /dev/null   * ''%%command 1>>/dev/null%%'': stdout go to /dev/null
   * ''%%command 2>>/dev/null%%'': Redirect stderr to /dev/null   * ''%%command 2>>/dev/null%%'': Redirect stderr to /dev/null
   * ''%%command 2>>&1%%'': Redirect stderr to stdout   * ''%%command 2>>&1%%'': Redirect stderr to stdout
-  * ''%%command &>>/dev/null%%'': Redirect stdout and stderr to /dev/null 
  
-=== File condition tests ===+=== 💻 File condition tests ===
  
-<code sh>+<code bash>
 if [[ -f filename ]]; then if [[ -f filename ]]; then
-  [...]+  # file exists
 else else
-  [...]+  # file not exists
 fi fi
 +# OR
 +[ -f ~/dotfiles/.bashrc ] && source ~/dotfiles/.bashrc
 </code> </code>
  
-  * ''-f file: is a regular file'' +  * ''%%-f%%'' is a regular file 
-  * -d file: is a directory +  * ''%%-d%%'' is a directory 
-  * -x file: execute permission +  * ''%%-x%%'' file: execute permission 
-  * -a file: file exists +  * ''%%-a%%'' file: file exists 
-  * -r file: have read permission +  * ''%%-r%%'' file: have read permission 
-  * -w file: have write permission +  * ''%%-w%%'' file: have write permission 
-  * -s file: file exists and is not empty +  * ''%%-s%%'' file: file exists and is not empty 
-  * file1 -nt file2: file1 is newer than file2 +  * file1 ''%%-nt%%'' file2: file1 is newer than file2 
-  * file1 -ot file2: file1 is older than file2+  * file1 ''%%-ot%%'' file2: file1 is older than file2
  
-=== Condition tests ===+=== 💻 check last command status code ===
  
-<code sh+<code bash
-if [[ -z $ENV_VAR ]]; then +if test $? = 0; then 
-  [...]+  echo 'do this';
 else else
-  [...]+  echo 'do that'; 
 +fi 
 +</code> 
 + 
 +=== 💻 Variable condition tests === 
 + 
 +<code bash> 
 +if [[ -z $? ]]; then 
 +  # last command run successfully 
 +  echo "status code is 0" 
 +else 
 +  echo "status code is not null, something wrong"
 fi fi
  
-[ -f ~/dotfiles/.bashrc ] && source ~/dotfiles/.bashrc 
-# or 
 [ -z "$ENV_VAR" ] && echo "ENV_VAR is empty" [ -z "$ENV_VAR" ] && echo "ENV_VAR is empty"
 [[ ! -z "$ENV_VAR" ]] && echo "ENV_VAR is not empty" || echo "ENV_VAR is empty" [[ ! -z "$ENV_VAR" ]] && echo "ENV_VAR is not empty" || echo "ENV_VAR is empty"
Line 52: Line 63:
   * ''%%string1 !== string2%%'' # string1 is not equal to string2   * ''%%string1 !== string2%%'' # string1 is not equal to string2
  
-== Conditionals ==+=== 💻 numberic conditionals ===
  
   * ''%%-lt%%'': Less than   * ''%%-lt%%'': Less than
Line 61: Line 72:
   * ''%%-ne%%'': Not equal to   * ''%%-ne%%'': Not equal to
  
-=== check if command available ===+=== 💻 check if command available ===
  
-<code sh>+<code bash>
 if !type brew &>/dev/null; then if !type brew &>/dev/null; then
   echo "brew install"   echo "brew install"
Line 77: Line 88:
 </code> </code>
  
-=== Test last command status code === +=== 💻 Manipulating and/or expanding variables ===
- +
-<code sh> +
-if test $? = 0; then +
-  echo 'do this'; +
-else +
-  echo 'do that'; +
-fi +
-</code> +
- +
-=== Manipulating and/or expanding variables ===+
  
 [[http://tldp.org/LDP/abs/html/parameter-substitution.html|parameter substitution]] [[http://tldp.org/LDP/abs/html/parameter-substitution.html|parameter substitution]]
Line 115: Line 116:
 </code> </code>
  
-=== Shortcuts ===+=== 💻 Shortcuts ===
  
   * CTRL L = Clear the screen   * CTRL L = Clear the screen
Line 127: Line 128:
   * !! = repeat last command   * !! = repeat last command
  
-=== For ===+=== 💻 For ===
  
-<code sh>+<code bash>
 for name [in list] for name [in list]
 do do
Line 136: Line 137:
 </code> </code>
  
-=== Case ===+=== 💻 Case ===
  
-<code sh>+<code bash>
 case expression in case expression in
   pattern1 )   pattern1 )
Line 147: Line 148:
 </code> </code>
  
-=== $() vs backtick ===+=== 💻 $() vs backtick ===
  
 [[http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_06_03|ref]] [[http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_06_03|ref]]
Line 153: Line 154:
 The backticks/gravemarks have been deprecated in favor of ''%%$()%%'' for command substitution. The backticks/gravemarks have been deprecated in favor of ''%%$()%%'' for command substitution.
  
-=== Special bash variables ===+=== 💻 Special bash variables ===
  
 [[http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html#table_03_03|ref]] [[http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html#table_03_03|ref]]
Line 169: Line 170:
 > The implementation of ''%%$*%%'' has always been a problem and realistically should have been replaced with the behavior of ''%%$@%%''. In almost every case where coders use ''%%$*%%'', they mean ''%%$@%%''. ''%%$*%%'' Can cause bugs and even security holes in your software. > The implementation of ''%%$*%%'' has always been a problem and realistically should have been replaced with the behavior of ''%%$@%%''. In almost every case where coders use ''%%$*%%'', they mean ''%%$@%%''. ''%%$*%%'' Can cause bugs and even security holes in your software.
  
-=== Search ===+=== 💻 Search ===
  
-<code sh>+<code bash>
 find . -iname "*filename*" find . -iname "*filename*"
 find . -type d find . -type d
 </code> </code>
  
bash/cheat.1737505080.txt.gz · Last modified: by dcai