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.
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.