Actions

 Language:
 RSS flow:


The test command


The test command is a unix command. It is not a shell builtin command. It can test the existence and status of a variable, or to make comparisons on variables, files, ...
Test returns 0 if the condition is verified. We call the command, via test or [].

Comparing integers

  • -eq

      equals
      if [ "$a" -eq "$b" ]
      will be true if the numbers contained in $a and $b variables are identical.
  • -ne

      is not equal to
      if [ "$a" -ne "$b" ]
      will be true when the numbers contained in $a and $b variables are not identical.
  • -gt

      is greater than
      if ["$a" -gt "$b" ]
      will be true when the number contained in $a variable is upper than the number contained in $b variable.
  • -ge

      is greater than or equal to
      if [ "$a" -ge "$b" ]
      will be true when the number contained in $a variable is greater than or equal to $b.
  • -lt

      is less than
      if [ "$a" -lt "$b" ]
      will be true when the number contained in $a variable is less than to $b.
  • -le

      is less than or equal to
      if [ "$a" -le "$b" ]
      will be true when the number contained in $a variable is less than or equal to $b.

Comparing strings

  • =

      equals
      if [ "$a" = "$b" ]
      will be true when $a and $b strings are identical.
  • !=

      is not equal to
      if [ "$a" != "$b" ]
      will be true when $a and $b strings are not identical.
  • <

      that is smaller, according to the alphabetical ASCII order
      if [[ "$a" < "$b" ]]
      if [ "$a" \< "$b" ]
      Note that < needs to be in an escape sequence if it is inside of [].
  • >

      is greater than, according to the alphabetical ASCII order
      if [[ "$a" > "$b" ]]
      if [ "$a" > "$b" ]
      Note that > needs to be in an escape sequence if it is inside of [].
  • -z

      The string is empty. That is to say that it has a zero size
      if [ -z "$a" ]
  • -n

      The string is not empty.
      if [ -n "$a" ]

Testing files

  • -e

      Check if file exists
      if [ -e "$fichier" ]
      will be true if the file exists.
  • -r

      Check if the file is readable
      if [ -r "$fichier" ]
      will be true if the file exists and is readable.
  • -w

      Checks if the file is writable
      if [ -w "$fichier" ]
      will be true if the file exists and is writable.
  • -d

      Checks if the directory exists
      if [ -d "$repertoire" ]
      will be true if the directory exists.

     

    Go Back