AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 801832
Accepted
Greg Dubicki
Greg Dubicki
Asked: 2016-09-09 03:49:09 +0800 CST2016-09-09 03:49:09 +0800 CST 2016-09-09 03:49:09 +0800 CST

普通 shell vs subshel​​l vs "bash -c" last command exit code

  • 772

我正在尝试正确链接&&最后一个命令的退出代码。||

我遇到了一个我无法解释的奇怪行为。请帮忙。

这是我的exit-code.sh测试脚本:

#!/bin/bash
echo "running exit-code with $1"
exit $1

这按预期工作:

$ ./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?
running exit-code with 1
last command exit code: 1

在()子shell中运行它也是如此:

$ (./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?)
running exit-code with 1
last exit code: 1

但:

$ /bin/bash -c "./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?"
running exit-code with 1
last exit code: 0

为什么我在这里得到退出代码 0?

bash shell
  • 2 2 个回答
  • 2051 Views

2 个回答

  • Voted
  1. Best Answer
    Anthony Geoghegan
    2016-09-09T05:18:55+08:002016-09-09T05:18:55+08:00

    为什么我在这里得到退出代码 0?

    原因是使用双引号时,$?在将参数传递给新的 Bash 进程之前执行参数扩展(特殊参数的)。如果你打开调试和详细模式,你可以清楚地看到这一点:

    $ set -xv
    $ bash -c "(./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?)"
    bash -c "(./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?)"
    + bash -c '(./exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: 0)'
    running exit-code with 1
    last exit code: 0
    $ set +xv
    

    在这种情况下$?设置为是0因为上一个命令(set -xv在本例中)成功执行。

    • 4
  2. XakRu
    2016-09-09T04:47:12+08:002016-09-09T04:47:12+08:00

    引号

    只需使用单引号

    /bin/bash -c './exit-code.sh 1 && ./exit-code.sh 2 && ./exit-code.sh 3 || echo last exit code: $?'
    
    running exit-code with 1
    last exit code: 1
    
    • 3

相关问题

  • Mac OS X:从 python 脚本中更改 $PATH

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • crontab ifconfig 什么都不输出

  • 使用命令行工具按排序顺序计算重复项

  • 是否有 bash 等效于 ruby​​ 的“一些内容#{foo}”?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve