dangyuluo
V2EX  ›  Bash

如何去除 Bash variable expansion 后加的单引号?

  •  
  •   dangyuluo · Jun 30, 2021 · 1800 views
    This topic created in 1777 days ago, the information mentioned may be changed or developed.

    例子如下

    $ set -x # enable debug output from bash
    $ export PYTEST_FLAG='--pytest-args -k "not (xmllint or pclint)"';
    $ pytest ${PYTEST_FLAG}
    + pytest --pytest-args -k '"not' '(xmllint' or 'pclint)"' # this line is the debug output
    ... error
    

    从第四行可以看出,本来"not (xmllint or pclint)"应该作为一个字符串传递给pytest命令,但是 bash 在 variable expansion 后把"not, (xmllintpclint)" 用单引号圈了起来,似乎因为他们含有一些特殊字符?如何阻止 bash 这种行为呢?谢谢

    我最终想要 Bash 执行的命令为:

    $ pytest --pytest-args -k "not (xmllint or pclint)"
    
    4 replies    2021-06-30 23:26:00 +08:00
    AoEiuV020
        1
    AoEiuV020  
       Jun 30, 2021
    叠个 bash,
    bash -c "pytest ${PYTEST_FLAG}"
    xuboying
        2
    xuboying  
       Jun 30, 2021
    declare -a PYTEST_FLAG=(--pytest-args -k "not (xmllint or pclint)")
    pytest "${PYTEST_FLAG[@]}"

    建议了解一下 shellcheck
    zbinlin
        3
    zbinlin  
       Jun 30, 2021
    这样?

    eval pytest "${PYTEST_FLAG}"
    dangyuluo
        4
    dangyuluo  
    OP
       Jun 30, 2021
    谢谢,eval 和`bash -c`都不被允许。

    @xuboying 不错!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   934 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    ♥ Do have faith in what you're doing.