I have a command that is returning a string with a leading whitespace that gets eaten when I use command substitution
> echo " test"
test
> echo $(echo " test")
test
Why is this happening and what can I do about it?
You can double-quote command substitution to preserve that,
echo "$(echo " test")"
test
From the man bash
on page,
Command Substitution
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.