bash - 命令替换省略空格
bash - command substitution is omitting whitespaces
我有一个命令返回一个带有前导空格的字符串,当我使用命令替换时它会被吃掉
> echo " test"
test
> echo $(echo " test")
test
为什么会发生这种情况,我该怎么办?
您可以用双引号替换命令以保留它,
echo "$(echo " test")"
test
来自第 man bash
页,
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.
我有一个命令返回一个带有前导空格的字符串,当我使用命令替换时它会被吃掉
> echo " test"
test
> echo $(echo " test")
test
为什么会发生这种情况,我该怎么办?
您可以用双引号替换命令以保留它,
echo "$(echo " test")"
test
来自第 man bash
页,
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.