I have a file called -. I want to display its contents.
- One way is to do
sincecat ./-cat -reads from standard input. - However, why are
cat "-"andcat '-'also interpreted by the shell ascat -?
I have a file called -. I want to display its contents.
cat ./-
since cat - reads from standard input.cat "-" and cat '-' also interpreted by the shell as cat -?The shell removes any quotes before cat sees them. So cat - and cat "-" and cat '-' all get passed through as the array ['cat', '-'] after whitespace tokenization, wildcard expansion, and quote removal by the shell.
Quotes are use by the shell, and not passed to the command:
e.g.
cat "hello world" #this passes the single token `hello world`, to `cat`, as argument 1. However the quotes are not passed.
cat "-" # this passes the single token `-`, to `cat`, as argument 1. The same as if no quotes had been used.
GNU cat is coded to compare the given filename against the string "-" before trying to open it as a file:
if (STREQ (infile, "-"))
{
have_read_stdin = true;
input_desc = STDIN_FILENO;
if (file_open_mode & O_BINARY)
xset_binary_mode (STDIN_FILENO, O_BINARY);
}
else
{
input_desc = open (infile, file_open_mode);
if (input_desc < 0)
{
error (0, errno, "%s", quotef (infile));
ok = false;
continue;
}
}
So, if you have a file named -, you need to defeat this logic by giving cat the path and name.
Quotation marks protect a value from white space splitting and, if single quotes, variable expansion. Quotation marks do not signal that the thing quoted is a file. To explicitly signal a value is a file, prefix it with a relative or absolute path.
All that said, one might suggest that GNU cat should also check if - is a file in the current working directory, but it'd be unusual for filenames to start with a hyphen or to be solely a hyphen, so that may be a performance concern. David Wheeler's essay, Fixing UNIX and Linux Filenames, has a nice discussion of this problem in historical context.