User Tools

Site Tools


bash_cheatsheet

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_cheatsheet [2014/05/27 11:34] ginkobash_cheatsheet [2019/02/13 16:07] (current) – [Output of a shell command] ginko
Line 19: Line 19:
     * ''${PARAMETER}''     * ''${PARAMETER}''
   * Indirection   * Indirection
-    * ''${!PARAMETER}''+    * ''${!PARAMETER}'' : Value of the variable whose name is ''PARAMETER''.
   * Case modification    * Case modification 
-    * ''${PARAMETER^}'' +    * ''${PARAMETER^}'' : First upper. 
-    * ''${PARAMETER^^}'' +    * ''${PARAMETER^^}'' : Whole upper. 
-    * ''${PARAMETER,}'' +    * ''${PARAMETER,}'' : First lower. 
-    * ''${PARAMETER,,}'' +    * ''${PARAMETER,,}'' : Whole lower. 
-    * ''${PARAMETER~}'' +    * ''${PARAMETER~}'' : Switch first. 
-    * ''${PARAMETER~~}''+    * ''${PARAMETER~~}'' : Switch all.
   * Variable name expansion   * Variable name expansion
-    * ''${!PREFIX*}'' +    * ''${!PREFIX*}'' : List of all variables and arrays names beginning with ''PREFIX''. 
-    * ''${!PREFIX@}''+    * ''${!PREFIX@}'' : Idem.
   * Substring removal (also for **filename manipulation**!)   * Substring removal (also for **filename manipulation**!)
-    * ''${PARAMETER#PATTERN}'' +    * ''${PARAMETER#PATTERN}'' : Strip left (short match). 
-    * ''${PARAMETER##PATTERN}'' +    * ''${PARAMETER##PATTERN}'' : Strip left (long match). 
-    * ''${PARAMETER%PATTERN}'' +    * ''${PARAMETER%PATTERN}'' : Strip right (short match). 
-    * ''<nowiki>${PARAMETER%%PATTERN}</nowiki>''+    * ''<nowiki>${PARAMETER%%PATTERN}</nowiki>'' : Strip right (long match).
   * Search and replace   * Search and replace
-    * ''${PARAMETER/PATTERN/STRING}'' +    * ''${PARAMETER/PATTERN/STRING}'' : Replace (fisrt occurence). 
-    * ''<nowiki>${PARAMETER//PATTERN/STRING}</nowiki>'' +    * ''<nowiki>${PARAMETER//PATTERN/STRING}</nowiki>'' : Replace (all occurences). 
-    * ''${PARAMETER/PATTERN}'' +    * ''${PARAMETER/PATTERN}'' : Search (first occurence). 
-    * ''<nowiki>${PARAMETER//PATTERN}</nowiki>''+    * ''<nowiki>${PARAMETER//PATTERN}</nowiki>'' Search (all occurrences).
   * String length    * String length 
     * ''${#PARAMETER}''     * ''${#PARAMETER}''
Line 46: Line 46:
     * ''${PARAMETER:OFFSET:LENGTH}''     * ''${PARAMETER:OFFSET:LENGTH}''
   * Use a default value   * Use a default value
-    * ''${PARAMETER:-WORD}'' +    * ''${PARAMETER:-WORD}'' : ''WORD'' when ''PARAMETER'' is unset or empty. 
-    * ''${PARAMETER-WORD}''+    * ''${PARAMETER-WORD}'' : ''WORD'' when ''PARAMETER'' is unset.
   * Assign a default value   * Assign a default value
-    * ''${PARAMETER:=WORD}'' +    * ''${PARAMETER:=WORD}'' : Expand AND assign ''WORD'' when ''PARAMETER'' is unset or empty. 
-    * ''${PARAMETER=WORD}''+    * ''${PARAMETER=WORD}'' : Expand AND assign ''WORD'' when ''PARAMETER'' is unset.
   * Use an alternate value   * Use an alternate value
-    * ''${PARAMETER:+WORD}'' +    * ''${PARAMETER:+WORD}'' : Nothing if ''PARAMETER'' is unset or empty, ''WORD'' else. 
-    * ''${PARAMETER+WORD}''+    * ''${PARAMETER+WORD}'' : Nothing if ''PARAMETER'' is unset, ''WORD'' else.
   * Display error if null or unset   * Display error if null or unset
-    * ''${PARAMETER:?WORD}'' +    * ''${PARAMETER:?WORD}'' : Like '':-'' AND sets non-null exit code and ''$?''. 
-    * ''${PARAMETER?WORD}''+    * ''${PARAMETER?WORD}'' : Like ''-'' AND sets non-null exit code and ''$?''.
  
 +==== Work with file paths ====
 +  * ''dirname=${fullpath%/*}''
 +  * ''filename=${fullpath##*/}''
 +  * ''extension=${filename##*.}''
 +  * <code bash>~% FILE="example.tar.gz"
 +~% echo "${FILE%%.*}"
 +example
 +~% echo "${FILE%.*}"
 +example.tar
 +~% echo "${FILE#*.}"
 +tar.gz
 +~% echo "${FILE##*.}"
 +gz</code>
 +==== Output of a shell command ====
 +  * ''$(mycmd params)''<code bash>mydate=$(date +%Y-%m-%d)
 +mytimestamp=$(date '+%Y-%m-%d %H:%M:%S')</code>
 ===== Mastering history ===== ===== Mastering history =====
 [[http://www.eriwen.com/bash/effective-shorthand/|Source]] [[http://www.eriwen.com/bash/effective-shorthand/|Source]]
Line 72: Line 88:
   * ''!:gs/foo/bar'' : last command with all occurrences of "foo" replaced with "bar"   * ''!:gs/foo/bar'' : last command with all occurrences of "foo" replaced with "bar"
   * ''<any_above>:p'' : prints command without executing   * ''<any_above>:p'' : prints command without executing
 +
 +===== Redirection =====
 +[[http://www.losurs.org/docs/redirection|Deux trois trucs intéressants sur les redirections]]
 +  * Diriger plusieurs lignes vers un fichier :<code bash>/bin/cat <<EOM >$FILE
 +text1
 +text2
 +text3
 +text4
 +EOM</code>
 +  * Normal:
 +    * ''binary > file'' (send stdout to file)
 +    * ''binary 2> file'' (send stderr to file)
 +    * ''binary > file 2>&1'' (send stdout and stderr to file)
 +    * ''binary < file'' (take stdin from file) 
 +  * Append:
 +    * ''binary %%>>%% file'' (send stdout to end of file)
 +    * ''binary 2%%>>%% file'' (send stderr to end of file)
 +    * ''binary %%>>%% file 2>&1'' (send stdout and stderr to end of file)
 +    * ''binary %%<<%%x'' (take stdin until "x" occurs) 
 +  * Pipes:
 +    * ''binary1 | binary2'' (pipe stdout of binary1 to stdin of binary2)
 +    * ''binary1 2>&1 | binary2'' (pipe stdout and stderr of binary1 to binary2) 
 +
 +
 +==== The problem with cats ... ====
 +
 +Take a simple redirection example such as this:
 +
 +''cat file1 | tr -d '\015' > file2''
 +
 +You've wasted a process on cat, since you could accomplish the same thing (and have it execute faster!) by doing this:
 +
 +''tr -d '\015' < file1 > file2''
 +
 +==== Sending stderr through a pipe ====
 +
 +Sending only stderr down a pipe, while having stdout still go to the screen, is an interesting trick. It can be done by passing the stdout and stderr file descriptors to temporary file descriptors, and basically playing a game of 3 card monte with the values:
 +
 +''(binary 3>&1 1>&2 2>&3 3>&-) | mail me@somewhere.org &''
 +
 +===== Fonctions =====
 +''func_name () { cmd1; cmd2 }''
 +
 +On retrouve cette syntax au coeur de la fameuse fork bomb bash '':(){ :|:& };:'' où '':'' est le nom de la fonction. L'utilisation du pipe et des '':'' est a priori seulement là pour obfusquer un peu plus la syntaxe ('':'') et la raccourcir (''|''). ''f(){f&;f&;};f'' fait quaisment la même chose.
 +
bash_cheatsheet.1401183296.txt.gz · Last modified: 2014/05/27 11:34 by ginko