Dashed filename in Linux

Working with dashed filename in Linux requires some attention.

Dash (-) character at the end of the commands is a popular convention to refer stdin or stdout. Dash is not a special character for filesystem or kernel.

But, if you accidently create a file which begins with a dash (-) character, you’ll be realize that you can’t simply delete this file with regular rm command:

rm -f -testfile
rm: invalid option -- t

Because of the first character is a dash, shell thinks we want to provide an option to rm command itself. To solve this problem you have to use double dash character -- after our command’s options are finished:

rm -f -- -testfile

When -- character seen, shell thinks that optional arguments for command itself finished and doesn’t make option parsing anymore.

Another solution for this problem, using ./ convention to refer filenames. For example you can repeat above example:

rm -f ./-testfile

This solution also works because command line option parsing ends when a ./ pattern received.

For the stdin and stdout effect, we can use /dev/stdin and /dev/stdout files. They are just symlinks to /proc/self/fd/0 and /proc/self/fd/1 so you can use like this:

echo test /dev/stdout
cat < /dev/stdin