In Unix-like systems, the double dash --
serves as a special argument that indicates the end of command options. This convention was introduced to handle filenames or arguments that might be misinterpreted as command options.
Consider this command structure:
kill cat -- $PIDFILE
The double dash protects against cases where $PIDFILE
might begin with a hyphen. For example, if your PID file were named -f
(unlikely but possible), without the double dash:
cat $PIDFILE # becomes cat -f
This would be interpreted as the -f
option for cat
rather than a filename.
Here are some practical examples showing why the double dash matters:
# Safe way to handle any filename
cat -- potentially_dangerous_filename.txt
# Problematic case without double dash
touch -- -rf
cat -rf # This would be interpreted as options
cat -- -rf # Correct way to handle it
While the double dash approach works universally, modern shells offer alternatives:
# Using $( ) instead of backticks
kill $(cat -- "$PIDFILE")
# Alternative approach with redirection
kill $(< "$PIDFILE")
- Always use double dash when processing variables that might contain filenames
- Quote your variables (
"$VAR"
) to prevent word splitting - Prefer
$( )
over backticks for command substitution - Consider using
kill "$(cat "$PIDFILE")"
for maximum safety
Watch out for these issues when working with PID files:
# Wrong: Missing quotes around variable
kill cat $PIDFILE
# Wrong: Missing double dash for safety
kill cat "$PIDFILE"
# Correct approach
kill "$(cat -- "$PIDFILE")"
When working with shell commands, you might encounter situations where filenames start with special characters like hyphens (-). The double dash (--) plays a crucial role in these cases by signaling the end of command options.
# Without double dash - problematic if PIDFILE starts with '-'
kill cat $PIDFILE
# With double dash - safe handling of any filename
kill cat -- $PIDFILE
Process ID (PID) files are commonly used in Unix/Linux systems to track running processes. While most PID files have simple names, some applications might generate them with special characters:
# Example of problematic filenames
-rwxr-xr-x.pid
--daemon-mode.pid
Without the double dash, the shell would interpret these as command options rather than filenames.
When processing command-line arguments:
- The shell first interprets options (flags starting with - or --)
- After encountering --, all following arguments are treated as operands
- This behavior is part of the POSIX standard
Here are some common scenarios where -- is essential:
# Safe file removal
rm -- -filename-with-hyphen.txt
# Secure grep operations
grep -- -search-term file.txt
# Proper find command usage
find . -name "*.txt" -- -exec rm {} \;
For PID file handling specifically, consider these robust patterns:
# Modern $( ) syntax with double dash
kill $(cat -- "$PIDFILE")
# Additional error checking
if [ -f "$PIDFILE" ]; then
kill -0 $(cat -- "$PIDFILE") 2>/dev/null && kill $(cat -- "$PIDFILE")
fi
While -- solves most issues, be aware of:
- Some older shells might not support --
- The feature must be implemented by the individual command (most core utilities support it)
- Always quote variables to handle spaces in filenames