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-03-21] dcaibash:cheat [2025-03-21] (current) dcai
Line 12: Line 12:
 === 💻 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>
  
Line 32: Line 34:
 === 💻 check last command status code === === 💻 check last command status code ===
  
-<code sh>+<code bash>
 if test $? = 0; then if test $? = 0; then
   echo 'do this';   echo 'do this';
Line 42: Line 44:
 === 💻 Variable condition tests === === 💻 Variable condition tests ===
  
-<code sh>+<code bash>
 if [[ -z $? ]]; then if [[ -z $? ]]; then
   # last command run successfully   # last command run successfully
Line 50: Line 52:
 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 74: Line 74:
 === 💻 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 130: Line 130:
 === 💻 For === === 💻 For ===
  
-<code sh>+<code bash>
 for name [in list] for name [in list]
 do do
Line 139: Line 139:
 === 💻 Case === === 💻 Case ===
  
-<code sh>+<code bash>
 case expression in case expression in
   pattern1 )   pattern1 )
Line 148: 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 154: 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 170: 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.1742553356.txt.gz · Last modified: by dcai