EastsideDeveloper EastsideDeveloper - 7 months ago 12
Bash Question

Seeing shell commands as they are being executed including variables

I can do

bash -x mysellh.sh


to see the commands as they are being executed, but I don't see the variables replaced with their values. For instance, if I have in the shell script:

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


I will see:

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


Even though I've already declared earlier:

R_ENV=test


Is there an option to do what I want?

Answer

In this case, it is doing what you asked: the command line "word" that $R_ENV is in is what you have enclosed in single quotes, which inhibit expansion. The -x output is showing you the non-expansion. If you want those variables to be expanded, enclose that first "word" in double quotes and use single quotes in the contents, like this:

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

Then the single quotes are around the expanded text and the double quotes allow the variables to expand.