保存命令输出保存换行符的字符串变量

时间:2013-01-23 16:27:52

标签: linux bash

我有一个转换文件数据的脚本,为了更有效地工作,我想改变内存中的数据,然后将其转储到文件中。 我希望修改包含此文件的文件:

> This is a line
> this is other line

我使用sed命令替换'>'带'#'的符号:

transform_output=$(eval "sed ${sed_args[@]} $file" 2>&1)
echo -e $transform_output

我得到了输出:

# This is a line # this is other line

而不是我想要的输出是:

# This is a line
# this is other line
  1. 如何将输出保存在保留换行符的字符串变量中?
  2. 如何在包含“#ADDED LINE#”的变量的开头添加一行并将其保存在文件中?
  3. (我想要获取的文件):

    #ADDED LINE#
    # This is a line
    # this is other line
    

    提前致谢

1 个答案:

答案 0 :(得分:5)

  1. 引用用于防止在空白处分词:

    echo -e "$transform_output"

  2. 将命令与另一个echo组合在一起:

    { echo "#ADDED LINE#"; echo -e "$transform_output" } > file