Linux-是否可以将控制台输出存储在变量中,但格式与控制台中存在的格式相同? [英] Linux - Is it possible to store console output in a variable but with the same format as its present in console?

查看:139
本文介绍了Linux-是否可以将控制台输出存储在变量中,但格式与控制台中存在的格式相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

好吧,我使用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

但是我需要 echo $ textInfo 给出类似1的输出。

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


推荐答案

使用 echo $ textInfo 代替 echo $ textInfo 。否则,变量将作为命令行的一部分而不是字符串扩展,因此不会保留换行符。

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.

这篇关于Linux-是否可以将控制台输出存储在变量中,但格式与控制台中存在的格式相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆