1.

Name the command that is used to compare the strings in a shell script.

Answer»

To compare text strings, use the test command. By COMPARING each character in each STRING, the test command usually COMPARES text strings.  In most cases, tests are generally included as a conditional execution of shell commands. It can be used for:

  • Comparison of file attributes
  • Comparison of strings
  • Comparing basic arithmetic operations

Using square brackets ([ and ]) around the EXPRESSION is equivalent to testing it with the test. 

Syntax of test command: 

  • test EXPRESSION
  • test EXPRESSION && true-command
  • test EXPRESSION || false-command
  • test EXPRESSION && true-command || false-command

Examples:  

  • test 50 -gt 40 && echo "Yes" || echo "No"

Because 50 is greater than 40, this command prints the text "Yes".

  • ["Excellent" = "Excellent"]; echo $?

As the two strings are identical, this command prints "0" because the expression is true.



Discussion

No Comment Found