bash -x 命令

标签 bash

我看到一个客户端执行 $bash -x 命令来查看文件是否可执行(或 ksh -x 命令等),就像 - x 在 shell 脚本的 if 语句中。

我的问题是:$bash -x 命令有什么作用?

我的解释是在当前 shell 中的新 bash shell 中启动命令,继承相同的环境变量并由相同的用户执行。

有趣的是我可以执行 $ls 但不能执行 $bash -x ls,它给出:

(在 AIX 6 下) /usr/bin/ls:/usr/bin/ls: 无法执行二进制文件

错误的原因对我来说是个谜 - 猜测这是由于特权,这意味着我上面的假设是不正确的。

此外,我相信 $bash ls$bash -x ls 是同一件事(-x 表示“执行”)?

非常感谢任何评论。

干杯!

最佳答案

-x 选项以跟踪模式启动 BASH shell。您可以看到有关如何处理您的命令/脚本的所有详细信息。如果您的脚本没有按照您的预期执行,这是找到一些错误的好方法

而且,正如亚历克斯所说in a comment ,要在 BASH 中运行命令,您必须使用 -c 选项,例如 bash -x -c ls

参见 man bash 或在线手册,特别是 invoking Bash 上的部分和 the set builtin command了解更多信息:

All of the single-character options used with the set builtin (see The Set Builtin) can be used as options when the shell is invoked.

-x

Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

关于bash -x 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107124/

相关文章:

python - 读取输出日志文件,并使用 bash/python 脚本打印所有唯一的文件路径

bash - AWK - 通过 for 循环和条件检查处理多个文件

bash - 从后台函数打印输出后如何返回 bash 提示符?

bash - ssh bash从远程文件接收变量

c - 自执行 C 程序或 Bash 文件

mysql - 将 sql 转储恢复到正在运行的 mysql/mariadb docker 容器中

mysql - 使用 MDB 工具从 Access 更新 MySQL 的 bash 脚本

linux - tar :cannot stat : No such file or directory when shell script run

macos - Snow Leopard 上的 .profile 和 .bash_profile 之间的区别

macos - 如何用一行运行多个 brew 命令?