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