EN
Bash - check if file is executable
0
points
In this article, we would like to show you how to check if file is executable in Bash.
Practical example
In this example, we use -x operator with specified path to check if file under the path is executable inside if statement.
script.sh file:
#!/bin/bash
path="/path/to/file.exe"
if [ -x "$path" ]; then
echo "The file is executable."
fi
Note: this approach should be used in POSIX shells where other solutions may not work.
Run in command line:
./script.sh
Output:
The file is executable.
Altermative notation
1. test based solution
#!/bin/bash
path="/path/to/file.exe"
if test -x "$path"; then
echo "The file is executable."
fi
Note:
[ ]notation is shorthand oftestcommand.
2. [[ ]] based solution
#!/bin/bash
path="/path/to/file.exe"
if [[ -x "$path" ]]; then
echo "The file is executable."
fi
Note:
[[ ]]notation is like extendedtestcommand, adding additional features (e.g. regular expressions).