Sunday, 4 December 2016

Shell Script Basics 4: compare two strings


This post explains the if-then-else usage of shell script with an example. Please note that the square bracket is equivalent with the 'test' command.

~/bin$ cat testingifthenelse
#!/bin/bash
#if test $1 = $2
if [ $1 = $2 ] # this format is equivalent with the above 'test' format
  then
  echo "The arguments are equal."
else
  echo "The arguments are NOT equal."
fi

I don't know whether the readers have noticed that  we use '=' to compare the two the two string $1 and $2, this is a different syntax with C/C++, which use '==' to compare strings. DO NOT confuse between them.





No comments:

Post a Comment