Difference between revisions of "Bash:Test"

From wiki
Jump to navigation Jump to search
(Created page with "The test command evaluates an expression and sets $? 0 => True/Successful 1 => False/Unsuccessful In if statement test can be executed as [ expression ]. This has better read...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
The test command evaluates an expression and sets $? 0 => True/Successful 1 => False/Unsuccessful
+
The test command evaluates an expression and sets $?
 +
:0 => True/Successful
 +
:1 => False/Unsuccessful
  
In if statement test can be executed as [ expression ]. This has better readability.
+
In if statements test can be executed as [ expression ]. This has better readability.
 +
 
 +
;if [ x${var} = x ]
 +
:Test if var is empty
  
Test commands
 
 
;-f filename
 
;-f filename
 
:Regular file existence
 
:Regular file existence
Line 12: Line 16:
 
;-s filename
 
;-s filename
 
:File is bigger than 0 bytes
 
:File is bigger than 0 bytes
 +
 +
;-z "string"
 +
:String is zero-length
 +
 +
;-n "string"
 +
:String is not zero-length

Latest revision as of 12:43, 11 May 2021

The test command evaluates an expression and sets $?

0 => True/Successful
1 => False/Unsuccessful

In if statements test can be executed as [ expression ]. This has better readability.

if [ x${var} = x ]
Test if var is empty
-f filename
Regular file existence
-e filename
Any file existence
-s filename
File is bigger than 0 bytes
-z "string"
String is zero-length
-n "string"
String is not zero-length