12

Is there any tools/tricks available for expanding variables to make bash code easier to read?

For example it would convert the following:

DIR=/var/tmp
FILE=${DIR}/file
SCRIPT_ROOT=/opt/root
TOOL=${SCRIPT_ROOT}/tool.sh
${TOOL} ${FILE}

to:

DIR=/var/tmp
FILE=/var/tmp/file
SCRIPT_ROOT=/opt/root
TOOL=/opt/root/tool.sh
/opt/root/tool.sh /var/tmp/file
  • I'm converting BASH code to Python and reading BASH syntax is helllla-tedious.
16

Run the script under bash -x:

$ bash -x script
+ DIR=/var/tmp
+ FILE=/var/tmp/file
+ SCRIPT_ROOT=/opt/root
+ TOOL=/opt/root/tool.sh
+ /opt/root/tool.sh /var/tmp/file

While -x is normally used for debugging, it seems to meet your needs.

Documentation

From man bash:

-x        Print commands and their arguments as they are executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.