在执行时看到 shell 个命令,包括变量

Seeing shell commands as they are being executed including variables

我能做到

bash -x mysellh.sh

看到正在执行的命令,但我没有看到变量被替换为它们的值。例如,如果我在 shell 脚本中有:

screen -d -m sh -c 'RAILS_ENV="$R_ENV" bundle exec rake sunspot:solr:reindex[500] > "$PROJECT_DIR"/solr/indexing_out.txt'

我会看到:

+ screen -d -m sh -c 'RAILS_ENV="$R_ENV" bundle exec rake sunspot:solr:reindex[500] > "$PROJECT_DIR"/solr/indexing_out.txt'

虽然我之前已经声明过:

R_ENV=test

是否可以选择做我想做的事?

在这种情况下,它正在执行您的要求:$R_ENV 所在的命令行 "word" 是您用单引号括起来的内容,它禁止扩展。 -x 输出显示了非扩展。如果要扩展这些变量,请将第一个 "word" 括在双引号中并在内容中使用单引号,如下所示:

screen -d -m sh -c "RAILS_ENV='$R_ENV' bundle exec rake sunspot:solr:reindex[500] > '$PROJECT_DIR'/solr/indexing_out.txt"

然后单引号围绕扩展文本,双引号允许变量扩展。