打印换行符
Printing a newline
GAAAAAAAAAAAAAAH 我不知道为什么我为此苦苦挣扎。
我只想做:
file="$a"[command-here]"$b"
制作$file=
a [newline]
b
然而无论我尝试使用什么 echo 或 printf,我都无法让它工作!!我只想要 a 和 b 之间的换行符。谁能把我从痛苦中解救出来?
file="$a""echo -e '\n'""$b" 行不通,我也想不出任何组合。
您可以使用:
a='foo'
b='bar'
file="$a"$'\n'"$b"
echo "$file"
foo
bar
或使用print -v
:
unset file
printf -v file "$a\n$b"
echo "$file"
foo
bar
sh
和类似的 shell 在带引号的字符串中保留换行符。你可以这样做:
file="$a
$b"
例如:
$ a=foo
$ b=bar
$ file="$a
> $b"
$ echo "$file"
foo
bar
GAAAAAAAAAAAAAAH 我不知道为什么我为此苦苦挣扎。
我只想做:
file="$a"[command-here]"$b"
制作$file=
a [newline]
b
然而无论我尝试使用什么 echo 或 printf,我都无法让它工作!!我只想要 a 和 b 之间的换行符。谁能把我从痛苦中解救出来?
file="$a""echo -e '\n'""$b" 行不通,我也想不出任何组合。
您可以使用:
a='foo'
b='bar'
file="$a"$'\n'"$b"
echo "$file"
foo
bar
或使用print -v
:
unset file
printf -v file "$a\n$b"
echo "$file"
foo
bar
sh
和类似的 shell 在带引号的字符串中保留换行符。你可以这样做:
file="$a
$b"
例如:
$ a=foo
$ b=bar
$ file="$a
> $b"
$ echo "$file"
foo
bar