我在Linux上使用bash shell。我有这个简单的脚本…

#!/bin/bash

TEMP=`sed -n '/'"Starting deployment of"'/,/'"Failed to start context"'/p' "/usr/java/jboss/standalone/log/server.log" | tac | awk '/'"Starting deployment of"'/ {print;exit} 1' | tac`
echo $TEMP

但是,当我运行这个脚本时

./temp.sh

打印的所有输出都没有回车/换行。不确定是否是我将输出存储到$TEMP,或echo命令本身的方式。

我如何存储命令的输出到一个变量,并保留换行/回车?


引用变量。原因如下:

$ f="fafafda
> adffd
> adfadf
> adfafd
> afd"

$ echo $f fafafda adffd adfadf adfafd afd $ echo "$f" fafafda adffd adfadf adfafd afd Without quotes, the shell replaces $TEMP with the characters it contains (one of which is a newline). Then, before invoking echo shell splits that string into multiple arguments using the Internal Field Separator (IFS), and passes that resulting list of arguments to echo. By default, the IFS is set to whitespace (spaces, tabs, and newlines), so the shell chops your $TEMP string into arguments and it never gets to see the newline, because the shell considers it a separator, just like a space.


我也遇到过同样的问题,引用一下会有帮助

ubuntu@host: ~ /应用程序美元= " abc > def” ubuntu@host:~/apps$ echo $apps abc def ubuntu@host:~/apps$ echo "$apps" 美国广播公司 def