Using Square Brackets in Bash

Square brackets ([ and ]) are essential components in Bash scripting. They are used for a variety of purposes, including conditional expressions, string and numerical comparisons, and file tests. This article provides a comprehensive overview of square brackets in Bash, exploring their uses and functionalities.

2. Overview of Square Brackets in Bash

In Bash, square brackets are used for test constructs. There are two main forms:

  1. Single square brackets [ ]
  2. Double square brackets [[ ]]

While single square brackets are POSIX compliant and widely used, double square brackets are more powerful and provide additional features specific to Bash.

3. Conditional Expressions with Square Brackets

Conditional expressions are used to test conditions and control the flow of the script based on the test results.

Example:

#!/bin/bash

if [ $1 -gt 10 ]; then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi

Output:

$ ./script.sh 15
The number is greater than 10.

$ ./script.sh 5
The number is 10 or less.

4. String Comparison Operators

String comparison operators are used to compare strings within square brackets.

Operators:

  • = : Equal to
  • != : Not equal to
  • -z : String is null (zero length)
  • -n : String is not null (non-zero length)

Example:

#!/bin/bash

if [ "$1" = "hello" ]; thenSquare brackets ([ and ]) are essential components in Bash scripting.
They are used for a variety of purposes, including conditional expressions, string and numerical comparisons, and file tests.
This article provides a comprehensive overview of square brackets in Bash, exploring their uses and functionalities.


echo "Hello, World!"
elif [ -z "$1" ]; then
echo "No input provided."
else
echo "Input is not 'hello'."
fi

Output:

$ ./script.sh hello
Hello, World!

$ ./script.sh
No input provided.

$ ./script.sh hi
Input is not 'hello'.

5. Numerical Comparison Operators

Numerical comparison operators are used to compare numbers within square brackets.

Operators:

  • -eq : Equal to
  • -ne : Not equal to
  • -lt : Less than
  • -le : Less than or equal to
  • -gt : Greater than
  • -ge : Greater than or equal to

Example:

#!/bin/bash

if [ $1 -eq 100 ]; then
echo "The number is 100."
elif [ $1 -lt 100 ]; then
echo "The number is less than 100."
else
echo "The number is greater than 100."
fi

Output:

$ ./script.sh 100
The number is 100.

$ ./script.sh 50
The number is less than 100.

$ ./script.sh 150
The number is greater than 100.

6. File Test Operators

File test operators are used to check properties of files.

Operators:

  • -e : File exists
  • -f : File is a regular file
  • -d : File is a directory
  • -r : File is readable
  • -w : File is writable
  • -x : File is executable

Example:

#!/bin/bash

if [ -e $1 ]; then
echo "File exists."
else
echo "File does not exist."
fi

Output:

$ ./script.sh /path/to/file
File exists.

$ ./script.sh /path/to/nonexistentfile
File does not exist.

7. Advanced Usage Examples

Advanced usage examples demonstrate more complex conditions and combinations of tests.

Example:

#!/bin/bash

if [ -d $1 ] && [ -r $1 ]; then
echo "Directory exists and is readable."
elif [ -f $1 ] && [ -w $1 ]; then
echo "File exists and is writable."
else
echo "File or directory does not meet conditions."
fi

Output:

$ ./script.sh /path/to/directory
Directory exists and is readable.

$ ./script.sh /path/to/file
File exists and is writable.

$ ./script.sh /path/to/nonexistentfile
File or directory does not meet conditions.

8. Best Practices and Tips

  • Always quote variables to prevent word splitting and globbing.
  • Use double square brackets for more complex conditions and to avoid some pitfalls of single square brackets.
  • Use parentheses to group conditions for readability and logical grouping.
  • Prefer -e over other file test operators when simply checking for existence.

9. Conclusion

Understanding how to use square brackets in Bash is crucial for writing effective and efficient scripts. This article covered the basics and advanced usage of square brackets, including string and numerical comparisons, file tests, and best practices.

Using Square Brackets in Bash – FAQs

What is the difference between single and double square brackets in Bash?

Single square brackets are POSIX compliant and widely used, while double square brackets are Bash-specific and offer more advanced features and improved syntax.

Why should I quote variables inside square brackets?

Quoting variables prevents word splitting and globbing, which can cause unexpected behavior and errors in your script.

Can I use multiple conditions inside square brackets?

A: Yes, you can use logical operators like && (AND) and || (OR) to combine multiple conditions within square brackets.