Shell | Snippet


Introduction

  • “#!”, defines shell to run script like #!/bin/sh

  • /bin/sh is not anymore bash, dash (Ubuntu>6.06)

    $ ls -al /bin/sh
    lrwxrwxrwx 1 root root 4  6M 30 19:44 /bin/sh -> dash
    


Operators

  • < : input redirection, to redirect the input given
  • > : output redirection, overwriting an already existing file or creates a new file if not exist.
  • >> : output redirection, appends an already existing file or creates a new file if not exist.


For loop

for i in (seq 0 2 10) ; do     
    echo "Running loop seq "$i # 0, 2, 4, 6, 8, 10
done


Special Characters

  • ~ : Home Directory
  • $ : allocate value of variable
  • & : for running background task
  • # : annotation
  • * : wild card
  • ? : wild card , single character
  • \ : generalize next character
  • ( : start child shell
  • ) : finish child shell
  • ! : NOT
  • ; : separate shell command
  • ``cmd` : reverse quotes, run command in command
  • | : use pipe command output as input of other command
  • [ : start string set
  • ] : finish string set ( #ls [ a-d]*.sh )