One liner echo using $(...)

Viewed 32

I can get the following to work:

self=$(readlink -f "${BASH_SOURCE[0]}")
echo "Sourcing $self"

But not the following (I want a one liner):

echo "Sourcing $(readlink -f \"${BASH_SOURCE[0]}\")"
2 Answers

Command substitution starts a new quoting context, so the quotes in the readlink command are not nested in the quotes surrounding the argument to echo. You don't need to escape them.

echo "Sourcing $(readlink -f "${BASH_SOURCE[0]}")"

You could also use

echo 'Sourcing' $(readlink -f "${BASH_SOURCE[0]}")
Related