How do I use GNU touch
to update a file called -
?
How do I use GNU cat
to display a file called -
?
I'm running:
% cat --version | head -n1
cat (GNU coreutils) 8.29
% touch --version | head -n1
touch (GNU coreutils) 8.29
Firstly, touch
:
% touch -
% ls -l
total 0
% touch -- -
% ls -l -- -
ls: cannot access '-': No such file or directory
Ok, I'll give up on creating a file with touch
. Let's create it with date
instead:
% date > -
% ls -l -
-rw-r--r-- 1 ravi ravi 29 Sep 8 19:54 -
%
Now, let's try to cat
it:
% cat -
% # I pressed ^D
% cat -- -
% # Same again - I pressed ^D
I know I can work around with:
% > -
and
% cat < -
But why don't these GNU utils support the convention that --
means that everything following is treated as a non-option?
How do I use these tools in the general case, for example I have a variable with the contents -
?
-
. A lone-
is not an option, so the issue is different from the issues in the questions that you have proposed as duplicates. – Kusalananda Sep 08 '18 at 14:04-
is going to be treated as an invalid option or a placeholder forstdin
orstdout
. In any case, the solution is the same. – Barmar Sep 08 '18 at 14:05-
is not an invalid option. – Kusalananda Sep 08 '18 at 14:10touch
andcat
do), and it's not a filename, and it's not an actual option, what else could it be? But this is immaterial, my point is that you use the same solution to deal with any filename beginning with-
, it doesn't matter why it doesn't work normally. – Barmar Sep 08 '18 at 14:14cat
) does, and if there is no file with the filename-
in the current directory, then the utility should complain about "no such file or directory". I'm totally OK with the dup votes though. – Kusalananda Sep 08 '18 at 15:06touch
doesn't complainno such file or directory
, since it creates new files. – Barmar Sep 08 '18 at 15:07touch
also recognise-
as special, and I said that if the utility didn't do that, then it may complain, especially if it treats it as the name of a file to read from. Unrelated to that, I'm noticing that BSDsort -o -
treats-
as stdout, while GNUsort
does not (while both implementations treatsort -
as reading from stdin). Interesting. – Kusalananda Sep 08 '18 at 15:20