Linux - Is it possible to store console output in a variable but with the same format as its present in console?

和自甴很熟 提交于 2019-12-11 20:46:10

问题


Here is my scenario:

well i use csh

1)

$ ls -l /corefiles/ | grep "root"
-rw-r----- 1 root  root   0 Sep 22  2014 core.3.4.
-rwxr-x--- 1 root  root  92 Sep 22  2014 ss.sh

2)

$ set textInfo=`ls -l /corefiles/ | grep "root"`
$ echo $textInfo
-rw-r----- 1 root root 0 Sep 22 2014 core.3.4. -rwxr-x--- 1 root root 92 Sep 22 2014 ss.sh

But I need echo $textInfo to give output like 1).

How can I achieve this? I do not want to redirect the content into a file. I need to store console output in a variable but with the same format as present in the console.

I need a variable which has content as below:

$ echo $textInfo
-rw-r----- 1 root  root   0 Sep 22  2014 core.3.4.
-rwxr-x--- 1 root  root  92 Sep 22  2014 ss.sh

回答1:


Use echo "$textInfo" instead of echo $textInfo. Otherwise the variable is expanded as part of the command line instead of as a string, so the newlines aren't preserved.




回答2:


try this:

textInfo=$(ls -l /corefiles/ | grep "root")

then

echo "$textInfo"



来源:https://stackoverflow.com/questions/24574245/linux-is-it-possible-to-store-console-output-in-a-variable-but-with-the-same-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!