Link Search Menu Expand Document

Common mistakes

  • [ 3 -eq 3 ] - spaces between [ ]
  • a=”hello there”.
      echo "$a" 
      if [ "$a" == "hello there" ]; then 
          echo "good"
      fi 
          # use "$a" is to distinguish the strings with space
    
  • Using $f.
      for f in *; 
      do 
          if [ -x "$f" ]; then
              echo "the file $f is executable"
          fi
      done
          # check file names, using "$f" is to also pick up file name with space 
    
  • Recognise ${a}.

      a="hello"
      b="yesterday"
      echo "${a}_today"
          # to recognise ${a} that is part of the string "_today"
      echo "${a}_good_!{b}"
    
  • Using single quote ‘’ is to preserve the variable as literal variable. Try below command in terminal, no need to create script file.

      a=hello
      echo ' the value is $a '
      echo " the value is $a "