linux - 如何使 Bash 命令将 "-"识别为文件名的一部分而不是选项?

标签 linux bash command-line

<分区>

How to make Bash commands recognize "-" as part of the filename and not an option?

我想在 Bash 中 hexdump 一个名为 -9 的文件的内容。因此,我使用以下命令:

hexdump -C \-9

在这里,我尝试转义 -,就像转义空格 一样。

但是,hexdump 给出了以下错误:

hexdump: invalid option -- '9'

也就是说,hexdump 将文件名解释为一个选项?

最佳答案

我已经能够使用 -- 来标记选项的结尾:

$ echo "test" > -9   # in this case it can't be understood as an option, no problem
$ hexdump -C -- -9   # -- marks the end of the options, so the -9 is understood as a filename
00000000  74 65 73 74 0a                                    |test.|
00000005

这是 getopt 的特殊性,如 its man page 中所述.

关于linux - 如何使 Bash 命令将 "-"识别为文件名的一部分而不是选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46785932/

相关文章:

PHP MongoDB 身份验证失败

c++ - 我如何从 Ocaml 调用 C++ 代码,使用它自己的共享库 .so ?

c++ - Boost 编译标志 gcc 错误

linux - 在 chromebook 开发者模式下安装 make

bash - Bash 中的 if、elif、else 语句问题

bash - 为什么打印的 null 与条件中的文字 null 的处理方式不同?

linux - bash - 尝试通过管道将命令运行到单独的函数时出错

bash - 将文件内容与命令输出进行比较,然后执行命令并追加文件

command-line - 无法启动带参数的程序

使用 CodeIgniter 2.xx 运行命令行时遇到 PHP 错误