Shell Scripting Quotes and Brackets - miraj/tools GitHub Wiki

Table of Contents

Double quote

In general double quote is for strings

  grep "sub testCase_" testcase.pm

Variable substitution happens within double quotes

  tc="12345"
  grep "sub testCase_$tc" testcase.pm
  is equivelent to 
  grep "sub testCase_12345" testcase.pm

Special characters are not special anymore in double quotes

They are considered as normal characters

 ls *
 ls args.p[lm]
 ls "*"
 ls "args.p[lm]"

double quoted string can contain single quote




Single quote

No variable substitution

No special characters

 num="10"
 echo '$num'
 echo "$num"

Back quote : command substitution

  grep 'sub ' code.pl | wc -l
  if you need the count in a variable
  lines=`grep 'sub ' code.pl | wc -l`

Command Result $?

  grep PASS log.txt
  echo $?

Command Grouping

Semi Colon ;

  sleep 5; echo 5 minutes passed
  gcc -o sendmessage sendmessage.c ; ./sendmessage -t -a 1; cat /tmp/message

and operator &&

  gcc -o sendmessage sendmessage.c && ./sendmessage -t -a 1 && cat /tmp/message

or operator ||

  echo $result | grep PASS || exit 1
  # code to handle success

Square brackets

Make sure there is space after [[and]]

 if [ [ "$status" == "PASSED" ] ]
 then
   release="yes"
 fi
 [ [ $num -lt 3 ] ] && exit 1;

brackets () grouping of commands and executing in a subshell

  (sleep 5; echo Done)
  (grep PASS log.txt; grep FAIL log.txt) > results.txt

Curly braces {}

 grep FAIL result.txt && { echo "Test case $tc failed";  exit 1; }

double bracket $(()) arithemetic expressions

 echo $((140 / 5 / 5))
 lines=`wc -l file.txt`
 files=`ls * | wc -l`
 total=$((lines*files))

converting output to file




              
⚠️ **GitHub.com Fallback** ⚠️