简体   繁体   English

Bash多线回声

[英]Bash multiline echo

I tried to create a script to list the contents of a directory: 我试图创建一个脚本来列出目录的内容:

#!/bin/bash
matched=$(ls -1 /data/ | grep $1)
echo $matched

I have added the parameter -1 to the ls command and when executed like this ./script dir the output is on one row: 我已经将参数-1添加到ls命令中,当像这样执行时./script dir输出在一行上:

dir1 dir2

I've also tried echo -e $matched , but the output was: 我也试过echo -e $matched ,但输出是:

-e dir1 dir2

So how can I list the directories each on separate line ? 那么如何在单独的行中列出每个目录呢?

Try using double quotes around the string to be echoed: 尝试在字符串周围使用双引号进行回显:

echo "$matched"

The quotes here cause certain special characters to be preserved; 这里的引号会导致某些特殊字符被保留; see here . 看到这里

Edit : See cdarke's comment for a better explanation. 编辑 :有关更好的解释,请参阅cdarke的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM