what exactly are the "[" and "test" commands?



You can write a test expression to different ways, man test.
1.
Code:
test EXPRESSION
2.
Code:
[ EXPRESSION ]
 
TEST COMMAND:
IT IS USED TO CHECK WHETHER THE GIVEN EXPRESSION IS TRUE OR FALSE
IT IS USED TO KNOW THE TYPE OF FILE AND THE PERMISSION
$ TEST
THE TEST COMMAND WILL RETURN THE EXIT STATUS AS 0 IF IT IS SUCCESS AND IT WILL RETURN 1 IF IT IS FALSE
FOR EXAMPLE: $ test 1 –eq 1
Then we need to use echo$? To check whether the condition is true or false
Now the answer should be zero as it valid
-àNow lets check giving some false condition
$test 1 –eq 2
$echo $?
The answer should be 1
Now we can compare two strings also
$ test “string” =”string”
The output will be 0
We can also check greater than or less than by placing –gt and –lt between the numbers
test 2 -gt 1 –a 3 –lt 1
Test command is also used to find whether the file is a directory or not
Test -f file1 //yes if file1 is regular file
Test -d dir //yes if dir is directory
Here in test command we can check multiple commands
test 1 -eq 1 -o 2 -eq 3 //here o denotes or condition
TRUE OR FALSE =TRUE, SO IT WILL RETURN 0

Now u can see the Command Snippet
1652967030264.png

[ ] Command
[] is similar to test command and only difference is we need to write to conditions in [] brackets


Syntax: [ condition ]
Then echo $?
1652967122221.png

Now lets see one of the what is [[]] command
In This [[]] we can use relational operators like >=,<=,<,>,=
Example: [[ 2 >5 ]]

It also supports && and || operators while we cant cant use them in [ ]
Code snippet
1652967208526.png


This are the differences between test and [] commands


+++++++++++++++++++
 

Members online


Latest posts

Top