Cygwin:'\ rm -fr'和'rm -fr'命令之间的区别?

DHR*_*SAL 5 linux shell cygwin

我在cygwin环境下的Windows环境中运行了一个shell脚本.此脚本具有一个清除功能,可根据特定条件删除系统上的某些文件夹.

我准备了要删除的所有文件夹的列表,然后使用以下命令:

rm -rfv $purge (where purge is the list of directories I want to delete)
Run Code Online (Sandbox Code Playgroud)

现在,当我测试这个脚本时,目录根本没有被删除.首先我认为清除列表存在一些问题,但是在调试时我才知道清除列表没问题.

经过大量的调试和试验后,我对命令做了一些小改动:

\rm -rfv $purge 
Run Code Online (Sandbox Code Playgroud)

它只是一种打击和试验,脚本开始工作正常.据我所知,\ rm和rm -f都意味着强制删除.

现在我怎么能证明为什么'rm -f'现在工作得更早,但'\ rm -f'的确如此.我想知道这两个命令之间的基本区别.

Mic*_*jer 9

rm可(理论上)之一:

  • shell builtin命令(但是我不知道有这样的内置shell)
  • 外部命令(最有可能/ bin/rm)
  • 一个shell函数
  • 别名

如果你把\它放在它之前(或引用它的任何部分,例如"rm"甚至'r'm)shell将忽略所有别名(但不是函数).

正如jlliagre所提到的,你可以问shell是什么rm以及\rm使用type内置的是什么.

实验:

$ type rm
rm is /bin/rm
$ rm() { echo "FUNC"; command rm "$@"; }
$ type rm
rm is a function
$ alias rm='echo ALIAS; rm -i'
$ type rm
rm is aliased to `echo ALIAS; rm -i'
Run Code Online (Sandbox Code Playgroud)

现在,我们有别名rm,功能rm和原始外部rm命令:让我们看看如何相互调用:

$ rm   # this will call alias, calling function calling real rm
$ rm
ALIAS
FUNC
rm: missing operand
$ \rm  # this will ignore alias, and call function calling real rm
FUNC
rm: missing operand
$ command rm  # this will ignore any builtin, alias or function and call rm according to PATH
rm: missing operand
Run Code Online (Sandbox Code Playgroud)

要深刻理解它,看help builtin, help command,help aliasman sh.


jll*_*gre 5

这意味着你的rm命令是别名或函数.反斜杠它告诉shell使用真正的rm命令.

编辑:您可以告诉命令rm引用的内容type,例如:

$ type rm
rm is /bin/rm
Run Code Online (Sandbox Code Playgroud)

.

$ type rm
rm is aliased to `rm -i'
Run Code Online (Sandbox Code Playgroud)

.

$ type rm
rm is a function
...
Run Code Online (Sandbox Code Playgroud)