Linux - Is it possible to store console output in

2019-09-15 08: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"



标签: linux unix csh