如何对以“-”开头的文件使用命令

Aar*_*ron 0 shell-script

我在对以“-”开头的文件(即 -file 1、-file 2)使用命令时遇到问题。它给了我无效的选项错误。

例如:

cp '-file 1' '-file 2'
Run Code Online (Sandbox Code Playgroud)

但是, echo 仍然有效。

echo "Hello World" > '-file 2'
Run Code Online (Sandbox Code Playgroud)

我怎样才能使“-”不表示我正在选择一个选项?

Chr*_*own 5

大多数 POSIX 实用程序指定--可用于终止选项参数:

cp -- '-file 1' '-file 2'
Run Code Online (Sandbox Code Playgroud)

您还可以使用.指向当前目录的硬链接来引用当前目录:

cp './-file 1' './-file 2'
Run Code Online (Sandbox Code Playgroud)