04-04-2019, 07:16 PM
Using Square Brackets in Bash: Part 2
![[Image: using-square-brackets-in-bash-part-2.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2019/04/using-square-brackets-in-bash-part-2.jpg)
![[Image: using-square-brackets-in-bash-part-2.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2019/04/using-square-brackets-in-bash-part-2.jpg)
Welcome back to our mini-series on square brackets. In the previous article, we looked at various ways square brackets are used at the command line, including globbing. If you’ve not read that article, you might want to start there.
Square brackets can also be used as a command. Yep, for example, in:
which is, by the way, a valid command that you can execute, is a command. Notice that there are spaces between the opening bracket and the parameters , and then between the parameters and the closing bracket . That is precisely because the brackets here act as a command, and you are separating the command from its parameters.
You would read the above line as “test whether the string “a” is the same as string “a”“. If the premise is true, the command finishes with an exit status of 0. If not, the exit status is 1. We talked about exit statuses in a previous article, and there you saw that you could access the value by checking the variable.
Try it out:
And now try:
In the first case, you will get a 0 (the premise is true), and running the second will give you a 1 (the premise is false). Remember that, in Bash, an exit status from a command that is 0 means it exited normally with no errors, and that makes it. If there were any errors, the exit value would be a non-zero value (). The command follows the same rules so that it is consistent with the rest of the other commands.
The command comes in handy in constructs and also in loops that require a certain condition to be met (or not) before exiting, like the and loops.
The logical operators for testing stuff are pretty straightforward:
You can also test for some very shell-specific things. The option, for example, tests whether a file exists or not:
If you run this in your test directory, line 3 will test to whether a file is in your long list of files. If it does exist, it will just print a message; but if it doesn’t exist, it will create it, to make sure the whole set is complete.
You could write the loop more compactly like this:
The modifier in the condition inverts the premise, thus line 3 would translate to “if the file does not exist“.
Try it: delete some random files from the bunch you have in your test directory. Then run the loop shown above and watch how it rebuilds the list.
There are plenty of other tests you can try, including tests to see if the name belongs to a directory and tests to see if it is a symbolic link. You can also test whether a files belongs to a certain group of users (), whether one file is older than another (), or even whether a file contains something or is, on the other hand, empty.
Try the following for example. Add some content to some of your files:
and then run this:
And you’ll remove all the files that are empty, leaving only the ones you added content to.
To find out more, check the manual page for the command (a synonym for ) with .
You may also see double brackets () sometimes used in a similar way to single brackets. The reason for this is because double brackets give you a wider range of comparison operators. You can use , for example, to compare a string to a pattern instead of just another string; or < and to test whether a string would come before or after another in a dictionary.
To find out more about extended operators check out this full list of Bash expressions.
Next Time
In an upcoming article, we’ll continue our tour and take a look at the role of parentheses in Linux command lines. See you then!
Read more:
![[Image: using-square-brackets-in-bash-part-2.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2019/04/using-square-brackets-in-bash-part-2.jpg)
![[Image: using-square-brackets-in-bash-part-2.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2019/04/using-square-brackets-in-bash-part-2.jpg)
Welcome back to our mini-series on square brackets. In the previous article, we looked at various ways square brackets are used at the command line, including globbing. If you’ve not read that article, you might want to start there.
Square brackets can also be used as a command. Yep, for example, in:
Code:
[ "a" = "a" ]which is, by the way, a valid command that you can execute,
Code:
[ ... ]Code:
[Code:
"a" = "a"Code:
]You would read the above line as “test whether the string “a” is the same as string “a”“. If the premise is true, the
Code:
[ ... ]Code:
$?Try it out:
Code:
[ "a" = "a" ]
echo $?And now try:
Code:
[ "a" = "b" ]
echo $?In the first case, you will get a 0 (the premise is true), and running the second will give you a 1 (the premise is false). Remember that, in Bash, an exit status from a command that is 0 means it exited normally with no errors, and that makes it
Code:
trueCode:
falseCode:
[ ... ]The
Code:
[ ... ]Code:
if ... thenCode:
whileCode:
untilThe logical operators for testing stuff are pretty straightforward:
Code:
[ STRING1 = STRING2 ] => checks to see if the strings are equal
[ STRING1 != STRING2 ] => checks to see if the strings are not equal [ INTEGER1 -eq INTEGER2 ] => checks to see if INTEGER1 is equal to INTEGER2 [ INTEGER1 -ge INTEGER2 ] => checks to see if INTEGER1 is greater than or equal to INTEGER2
[ INTEGER1 -gt INTEGER2 ] => checks to see if INTEGER1 is greater than INTEGER2
[ INTEGER1 -le INTEGER2 ] => checks to see if INTEGER1 is less than or equal to INTEGER2
[ INTEGER1 -lt INTEGER2 ] => checks to see if INTEGER1 is less than INTEGER2
[ INTEGER1 -ne INTEGER2 ] => checks to see if INTEGER1 is not equal to INTEGER2
etc...You can also test for some very shell-specific things. The
Code:
-fCode:
for i in {000..099}; \ do \ if [ -f file$i ]; \ then \ echo file$i exists; \ else \ touch file$i; \ echo I made file$i; \ fi; \
doneIf you run this in your test directory, line 3 will test to whether a file is in your long list of files. If it does exist, it will just print a message; but if it doesn’t exist, it will create it, to make sure the whole set is complete.
You could write the loop more compactly like this:
Code:
for i in {000..099};\
do\ if [ ! -f file$i ];\ then\ touch file$i;\ echo I made file$i;\ fi;\
doneThe
Code:
!Code:
file$iTry it: delete some random files from the bunch you have in your test directory. Then run the loop shown above and watch how it rebuilds the list.
There are plenty of other tests you can try, including
Code:
-dCode:
-hCode:
-GCode:
-otTry the following for example. Add some content to some of your files:
Code:
echo "Hello World" >> file023
echo "This is a message" >> file065
echo "To humanity" >> file010and then run this:
Code:
for i in {000..099};\
do\ if [ ! -s file$i ];\ then\ rm file$i;\ echo I removed file$i;\ fi;\
doneAnd you’ll remove all the files that are empty, leaving only the ones you added content to.
To find out more, check the manual page for the
Code:
testCode:
[ ... ]Code:
man testYou may also see double brackets (
Code:
[[ ... ]]Code:
==Code:
>To find out more about extended operators check out this full list of Bash expressions.
Next Time
In an upcoming article, we’ll continue our tour and take a look at the role of parentheses
Code:
()Read more:
- The Meaning of Dot ()Code:
.
- Understanding Angle Brackets in Bash ()Code:
<...>
- More About Angle Brackets in Bash(andCode:
<)Code:>
- And, Ampersand, and & in Linux ()Code:
&
- Ampersands and File Descriptors in Bash ()Code:
&
- Logical & in Bash ()Code:
&
- All about {Curly Braces} in Bash ()Code:
{}
- Using Square Brackets in Bash: Part 1

