You are on page 1of 2

BASH cheat sheet - Level 2 ${variable:N:length}

  Wildcards operators
Substring   the   value   contained   by   variable   from  
thr  Nth  character  to  up  to  length  specidied.   Regular  expressions  :   Used  to  match  text.  
Miscellaneous ${variable/pattern/string}    
The   longest   match   of   pattern   against   the   variable   ^     Matches  the  beginning  of  the  line.  
\    Escape  character.  It  preserves  the  literal  value  of   value  is  replaced  with  string.   $     Matches  the  end  of  the  line.  
the   next   character   that   follows,   with   the   ^$   Matches  blank  lines.  
exception  of  newline. Print commands .     Any  character.  
[]     Any  of  the  character  inside  the  brackets.  
`  command`                  The  backtick  (`)  is  a  command  substitution.     echo  My  home  is:  $HOME     Write  arguments  to  the     [^a-­‐f]     Matches  any  character  except  those  in  the  range  
echo  The  current  working  directory  is:  `pwd`   >My  home  is:  /home/user     standard  output.   a  to  f.  
>The  current  working  directory  is:  /home/user/path     \a     A  letter  (similar  to  [a-­‐zA-­‐Z]).  
  echo  –e     Enable   interpretation   of   backslash-­‐ \t   A  tabulation.  
The  text  between  a  pair  of  backtick  is  executed  by  the   escaped  characters. \n     A  new  line.  
shell   before   the   main   command   and   is   then   replaced   printf   Format  and  print  the  arguments.   \w   An  alphanumeric  ([a-­‐zA-­‐Z0-­‐9_])  
by   the   output   of   that   execution.   The   syntax   \W   Non  alphanumeric  (The  opposite  of  \w)  
 
$(command)  is  generally  preferable.   ?         The  preceding  item  matches  0  or  1  time.  
printf  %q  "$IFS"                      Print  the  arguments  shell-­‐quoted.  
  >'  \t\n'     *     The  preceding  item  matches  0  or  more  times.  
$     It   introduces   parameter   expansion,   command     +   The  preceding  item  matches  1  or  more  times.  
substitution,   or   arithmetic   expansion.   The   printf  "%.1f"  2.558     Specify  the  decimal  precision.   {N}   The  preceding  item  matches  exactly  N  times.  
parameter    name    or    symbol  to  be  expanded  may   >2.6   {N,}   The  preceding  item  matches  N  times  or  more.  
be  enclosed  in  braces.     {N,M}   The  preceding  item  matches  at  least  N  times  and  
printf  "%s\t%s\n"  "1"  "2"  "3"  "4"                          %s  interprets  the     not  more  than  M  times.  
Using variables >1   2              associated  argument    [:class:]   POSIX   Character   Classes   ([:alnum:],   [:alpha:],  
   3   4              literally  as  string.   [:blank:],   [:digit:],   etc,   respectively   equivalent   to  
A-­‐Za-­‐z0-­‐9,  A-­‐Za-­‐z,  space  or  a  tab,  0-­‐9,  etc).    
variable=value  
 
Assign  a  value  value  to  the  variable  variable.  The   Using quotes
Globbing  (Pathname  expansion)  :  
variable  scope  is  restricted  to  the  shell.  
                     Used  to  match  filename(s).  
local  variable=value   Weak  quoting  -­‐  double  quote  (")  :   ?   Any  single  character  
Assign  a  value  value  to  the  local  variable  variable.     *   Zero  or  more  characters  
It  doesn’t  come  out  a  curly  bracket  area.   string="My  home  is:  $HOME"   []   Specify   a   range.   Any   character   of   the   range   or  
export  variable=value   echo  $string   none  of  them  by  using  !  inside  the  bracket.  
Make   the   variable   name   available   to     the   shell   >My  home  is:  /home/user   {term1,term2}   Specify   a   list   of   terms   separated   by  
and  sub-­‐processes.  
Use   when   you   want   to   enclose   variables   or   commas   and   each   term   must   be   a   name   or   a  
variable=$(command)   use  shell  expansion  inside  a  string.     wildcard.  
    Assign  the  output  of  command  to  variable.  
{term1..term2}   Called   brace   expansion,   this   syntax  
${#variable}
Strong  quoting  -­‐  single  quote  (')  :   expands   all   the   terms   between  term1   and  term2  
Length  of  the  value  contained  by  the  variable.  
  (Letters  or  Integers).  
${variable:N}  
echo  'My  HOME  is:  $HOME'    
Keep   the   character   of   the   value   contained   by  
>My  HOME  is:  $HOME   With  the  extglob  shell  option  enabled  (check  it  with  shopt)  :  
variable  after  the  Nth.  
Preserves   the   literal   value   of   each   character   In   the   following   description,   a  pattern-­‐list   is   a   list  of  one  
 
within  the  quotes.   or  more  patterns  separated  by  a  |.  
 
man  command  :  display  the  command’s  manual  page  
Jacques  Dainat  -­‐  2015  
  Math calculation
?(pattern-­‐list)   Matches   zero   or   one   occurrence   of   the   $0  is  an  entire  line.  
given  patterns.   $1  is  the  first  field,  $2  the  second,  etc.   +   Plus  
*(pattern-­‐list)   Matches  zero  or  more  occurrences  of  the     +=   Plus-­‐equal  (increment  variable  by  a  constant)  
given  patterns.   By   default,   fields   are   separated   by   white   space.   Use   -­‐   Minus.  
+(pattern-­‐list)   Matches  one  or  more  occurrences  of  the   the   –F   option   to   define   the   input   field   separator   (can   -­‐=   Minus-­‐equal  (decrement  variable  by  a  
given  patterns.   be  a  regular  expression).   constant).  
@(pattern-­‐list)   Matches  one  of  the  given  patterns.     *   Multiplication.  
    NF   Number  of  fields  in  the  current  record.   *=   Times-­‐equal  (multiply  variable  by  a  
!(pattern-­‐list)     Matches   anything   except   one   of   the     NR   Ordinal  number  of  the  current  record.   constant).  
given  patterns.     FNR   Ordinal   number   of   the   current   record   in   /   Division.  
  the  current  file.   /=   Slash-­‐equal  (divide  variable  by  a  constant).  
/!\  Regular  expressions  and  globbing  wildcards  should     %     Modulo  (returns  the  remainder  of  an  integer  
not  be  mixed  up.  They  have  different  meaning.   -­‐v  name=$var       It  allows  to  pass  the  shell   division  operation).  
variable  $var  to  awk  command.  The  variable   %=   Modulo-­‐equal  (remainder  of  dividing  variable  
File modification commands is  known  as  name  within  the  awk  command.   by  a  constant).  
**   Exponentiation.  
tr  string1  string2  <  file   awk  '{  if  ($2  ~  pattern)  arr[$0]++}  END  {  for  (i  in   ++     Increment  a  variable  by  1.  
Replace   string1   characters   occurrences   within   file   by   arr){print  $i}  }'  file   -­‐-­‐   Decrement  a  variable  by  1.  
string2   characters   (where   the   first   character   in     For  each  line  where  the  second  field  match    
string1  is  translated  into  the  first  character  in  string2   the   p attern,   save  the  line  as  key  in  the  associative   (( var = operation )) or var=$(( operation ))
and  so  on).   array   a rr   a nd   increment  its  value.  At  the  end  print   Assign   the   result   of   an   arithmetic   evaluation  
  each   k ey   o f   t he   associative  array.  This  will  remove  the   to  the  variable  var.
sed  is  a  non-­‐interactive  text  file  editor  :   duplicate   l ines   t hat  have  matched.  
    /  !\   Natively Bash   can   only   handle   integer  
sed  's/pattern1/pattern2/g’  ficOrigine   awk  'FNR==NR{arr[$4]++;next}{  if($4  in  arr)print   arithmetic.  
Replace   pattern1   occurrence   within   file   by   pattern2.   $0  }'  file1  file2  
The   s   means   «  substitute  »   and   the   g   means   «  global   Print  all  lines  of  file2  where  the  fourth  field  
Floating-­‐point  arithmetic  
replacment  »  (Not  only  the  first  occurence).   matches  one  of  the  third  field  of  file1.  
You   must   delegate   such   kind   of   calcul   to   specific  
-­‐e  :  allows  combining  multiple  commands  (use  a  -­‐e  before   command  line  tool  as  bc.  
each  command).   String commands together  
-­‐i  :  Edit  files  in-­‐place.  (Be  carefull  using  that  option)   echo "operation" | bc –l
sed  -­‐n  5,10p  file   command  <  file     Display the result of a floating-point
  Print  lines  5  to  10.   Redirect   file   into   a   command.   File   is   read   as   arithmetic.
standard  input  instead  of  the  terminal  command.  
var=$(echo "operation " | bc -l)
The awk command  
Assign the floating-point arithmetic result
command1  |  command2  
Connect  the  standard  output  of  the  left  command   to the variable var.
awk  is  a  field-­‐oriented  pattern  processing  language.  
  to  the  standard  input  of  the  right  command.  
 
awk 'BEGIN { Initial command(s) }
command1  ;  command2
{ by line command(s) } Separate   two   commands.   Permit   putting  
END { final command(s) }' file several  commands  on  the  same  line.  
man  command  :  display  the  command’s  manual  page  
Jacques  Dainat  -­‐  2015  

You might also like