Cat 重定向不起作用

Cat 重定向不起作用

我使用了一个命令

set -r noclobber

现在我无法重置。cat >1.txt显示Restricted: can't redirect output

答案1

它似乎没有在内置命令下列出set,但set -r似乎bash与使用-r命令行选项调用具有相同的效果:即,如下所述OPTIONS

   -r        If  the  -r  option  is present, the shell becomes restricted
             (see RESTRICTED SHELL below).

下面RESTRICTED SHELL你会看到

RESTRICTED SHELL
       If bash is started with the name rbash, or the -r option is supplied at
       invocation, the shell becomes restricted.  A restricted shell  is  used
       to  set  up an environment more controlled than the standard shell.  It
       behaves identically to bash with the exception that the  following  are
       disallowed or not performed:

其中一项限制是

   ·      redirecting output using the >, >|, <>, >&, &>, and >> redirect‐
          ion operators

这解释了为什么您会看到Restricted: can't redirect output

通常情况下,当使用 设置 选项时set -,可以使用等效的 来关闭该选项set +。但是,正如您所预料的,模式限制的一件事RESTRICTED SHELL

   ·      turning off restricted mode with set +r or set +o restricted.

所以,据我所知,您唯一的选择是启动一个新的、不受限制的 shell。


选项set -o noclobber(或等效的set -C)也会影响重定向,但-r与仅当目标文件存在时不同:

          -C      If set, bash does not overwrite an  existing  file  with
                  the  >,  >&,  and <> redirection operators.  This may be
                  overridden when creating output files by using the redi‐
                  rection operator >| instead of >.

也不同于的是-r,这个可使用set +C(或set +o noclobber)切换。

相关内容