我正在尝试这个,它完美地工作
echo "This is a very long string. An"\ "d it is printed in one line" Output: This is a very long string. And it is printed in one line
但是当我尝试缩进它时,echo语句也缩进了.它增加了额外的空间.
echo "This is a very long string. An"\ "d it is printed in one line" Output: This is a very long string. An d it is printed in one line
我找不到任何可以完美做到这一点的工作反应.
解决方法
$echo "a" "b" a b $echo "a" "b" a b $echo "a"\ > "b" a b
如果要完全控制要打印的内容,请使用带有printf的数组:
lines=("This is a very long string. An" "d it is printed in one line") printf "%s" "${lines[@]}" printf "\n"
这将返回:
This is a very long string. And it is printed in one line
或者作为suggested by 123 in comments,使用echo也将数组设置IFS设置为null:
# we define the same array $lines as above $IFS="" $echo "${lines[*]}" This is a very long string. And it is printed in one line $unset IFS $echo "${lines[*]}" This is a very long string. An d it is printed in one line # ^ # note the space
从Bash manual → 3.4.2. Special Parameters开始:
*
($) Expands to the positional parameters,starting from one. When the expansion is not within double quotes,each positional parameter expands to a separate word. In contexts where it is performed,those words are subject to further word splitting and pathname expansion. When the expansion occurs within double quotes,it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is,“$” is equivalent to “$1c$2c…”,where c is the first character of the value of the IFS variable. If IFS is unset,the parameters are separated by spaces. If IFS is null,the parameters are joined without intervening separators.
有趣的阅读:Why is printf better than echo?.