On linux systems when you type a command in a shell like rm * -rf
, the order of the *
and the -rf
doesn't matter. My shell interpret it the same way. Now, on my Mac when I type rm -rf *
everything works fine, but if I do rm * -rf
an error shows up rm: -rf: No such file or directory
在linux系统上,当您在rm * -rf这样的shell中输入命令时,*和-rf的顺序并不重要。我的壳以同样的方式解释它。现在,在我的Mac上,当我输入rm -rf *时,一切正常,但是如果我输入rm * -rf,一个错误就会显示rm: -rf:没有这样的文件或目录
I tried that on a macOS and a linux both with fish and bash shells. Same problems.
我在macOS和linux上都试过,有fish和bash shell。同样的问题。
Does anyone have any idea why the command interpreter on macOS thinks that -rf
at the end of the command is not interpreted as parameters of the command ?
有人知道为什么macOS上的命令解释器认为命令末尾的-rf不被解释为命令的参数吗?
2 个解决方案
#1
2
It's not about the shell, it's about the commands. The parsing of command line arguments is not a feature and responsibility of the shell, but of the actual commands. In both systems the shell faithfully passes the command line arguments in whatever order they were specified, and then it's up to the implementation of the commands to parse them as they see fit.
这不是shell的问题,而是命令的问题。命令行参数的解析不是shell的特性和职责,而是实际的命令。在这两个系统中,shell忠实地按照指定的顺序传递命令行参数,然后由命令的实现来解析它们。
In linux, the core utilities are typically of the GNU implementation, while on osx, the core utilities are typically of the BSD implementation.
在linux中,核心实用程序通常是GNU实现,而在osx中,核心实用程序通常是BSD实现。
The man
page of the commands should tell you which implementation it is.
命令的手册页应该告诉您它是哪个实现。
For example the last line of man rm
in Linux is something like this:
例如,Linux中的最后一行man rm是这样的:
GNU coreutils 8.21 March 2016 RM(1)
On osx:
在osx上:
BSD January 28, 1999 BSD
#2
1
Order of the arguments in any shell has historically been relevant in unix.
任何shell中的参数的顺序在unix中都是相关的。
rm incidentally even has an option -- to stop parsing options (to be able to remove files that start with "-" e.g.)
顺便说一句,rm还有一个选项——停止解析选项(可以删除以“-”开头的文件)。
See rm(1) and getopt(3) man pages
参见rm(1)和getopt(3)手册页
if the shell doesn't respect order of the arguments it is given just what would the result be of this sequence:
如果壳层不考虑参数的顺序它给出的是这个序列的结果是什么
$ touch a b
$ mv a b
what file would remain ?
还剩下什么文件?
#1
2
It's not about the shell, it's about the commands. The parsing of command line arguments is not a feature and responsibility of the shell, but of the actual commands. In both systems the shell faithfully passes the command line arguments in whatever order they were specified, and then it's up to the implementation of the commands to parse them as they see fit.
这不是shell的问题,而是命令的问题。命令行参数的解析不是shell的特性和职责,而是实际的命令。在这两个系统中,shell忠实地按照指定的顺序传递命令行参数,然后由命令的实现来解析它们。
In linux, the core utilities are typically of the GNU implementation, while on osx, the core utilities are typically of the BSD implementation.
在linux中,核心实用程序通常是GNU实现,而在osx中,核心实用程序通常是BSD实现。
The man
page of the commands should tell you which implementation it is.
命令的手册页应该告诉您它是哪个实现。
For example the last line of man rm
in Linux is something like this:
例如,Linux中的最后一行man rm是这样的:
GNU coreutils 8.21 March 2016 RM(1)
On osx:
在osx上:
BSD January 28, 1999 BSD
#2
1
Order of the arguments in any shell has historically been relevant in unix.
任何shell中的参数的顺序在unix中都是相关的。
rm incidentally even has an option -- to stop parsing options (to be able to remove files that start with "-" e.g.)
顺便说一句,rm还有一个选项——停止解析选项(可以删除以“-”开头的文件)。
See rm(1) and getopt(3) man pages
参见rm(1)和getopt(3)手册页
if the shell doesn't respect order of the arguments it is given just what would the result be of this sequence:
如果壳层不考虑参数的顺序它给出的是这个序列的结果是什么
$ touch a b
$ mv a b
what file would remain ?
还剩下什么文件?