用于调试的 shell 选项 -v 和 -x 有什么区别?

KM1*_*646 8 debugging bash shell zsh sh

-x表示 xtrace,-v表示详细。

在研究时,我发现bash -x“扩展命令”同时-v在执行之前将整个命令打印到标准输出。

我在测试时看到的唯一区别是它bash -v <scriptname>也会显示每个命令的注释。

这真的是唯一的区别吗?有人可以详细说明吗?

小智 8

从手册中:

  -v  Print shell input lines as they are read.
  -x  Print commands and their arguments as they are executed.
Run Code Online (Sandbox Code Playgroud)

当查看管道命令的处理方式时,您可以看到这种区别:

$ set -v
$ echo "Hello World" | sed 's/World/Earth/'
echo "Hello World" | sed 's/World/Earth/'
Hello Earth
Run Code Online (Sandbox Code Playgroud)

相对:

$ set -x
$ echo "Hello World" | sed 's/World/Earth/'
    + sed s/World/Earth/
    + echo 'Hello World'
    Hello Earth
Run Code Online (Sandbox Code Playgroud)

此外,xtrace (-x) 似乎使用 $PS4 变量,而 verbose (-v) 则不使用。