EN
Bash - check if file is socket
0
points
In this article, we would like to show you how to check if file is socket in Bash.
Practical example
In this example, we use -S operator with specified path to check if file under the path is socket inside if statement.
script.sh file:
#!/bin/bash
path="/path/to/socket"
if [ -S "$path" ]; then
echo "The file is socket."
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 socket.
Altermative notation
1. test based solution
#!/bin/bash
path="/path/to/socket"
if test -S "$path"; then
echo "The file is socket."
fi
Note:
[ ]notation is shorthand oftestcommand.
2. [[ ]] based solution
#!/bin/bash
path="/path/to/socket"
if [[ -S "$path" ]]; then
echo "The file is socket."
fi
Note:
[[ ]]notation is like extendedtestcommand, adding additional features (e.g. regular expressions).