我有一个 bash 脚本。如果我运行此命令:
然后我在终端中每行找到一个 grep find
LC_ALL=C grep -o --color -h -E -C 0 -r "$pattern" /pathto/Pre_N/ | tr -d '[:digit:]' | sort | uniq
Run Code Online (Sandbox Code Playgroud)
但是,如果我把它放在一个输出变量中,那么格式就会丢失(在回显到文件或在屏幕上回显时)。
#!/usr/bin/env bash
output=$(LC_ALL=C grep -o --color -h -E -C 0 -r "$pattern" /pathto/Pre_N/ | tr -d '[:digit:]' | sort | uniq)
echo $output > $fn
Run Code Online (Sandbox Code Playgroud)
将此命令保存到变量后,如何保留该命令的输出格式?
Pie*_*res 26
使用引号:
echo "$output" > $fn
Run Code Online (Sandbox Code Playgroud)
echo 行由 shell 解析(在变量扩展之后),并且参数之间的所有空格字符都减少为一个简单的空格。例如:
$ var1="toto
> titi
> tutu
> "
$ echo $var1
toto titi tutu
$ echo "$var1"
toto
titi
tutu
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28342 次 |
| 最近记录: |