简体   繁体   English

bash中的换行符

[英]new line character in bash

I am currently writing a bash scipt where i need to concatenate the results within the output variable. 我目前正在写一个bash scipt,我需要在输出变量中连接结果。 However I need them to be seperated by newline charcater '\\n' which does not seem to work... Any ideas ??? 但是我需要用换行符'\\ n'分隔它们,这似乎行不通...任何想法?

#!/bin/bash

for i in "./"*
do

#echo "$i"
tmp=$('/home/eclipseWorkspace/groundtruthPointExtractor/Debug/groundtruthPointExtractor' "./"$i) 
#echo $Output
#printf "$i $Output\n">> output.txt
Output=$Output$(printf $'\n %s %s' "$i" "$tmp" )
done
echo $Output
echo $Output> output.txt

Well looks like 看起来像

echo "$str" works 回声“ $ str”作品

because when you print the string without quotes, newline are converted to spaces !!! 因为当您打印不带引号的字符串时,换行符会转换为空格!

You can skip accumulating the output in a single parameter with 您可以使用以下命令在单个参数中跳过累加输出

DIR=/home/eclipseWorkspace/groundtruthPointExtractor/Debug
for i in *; do
    printf "%s %s\n" "$i" "$("$DIR/groundtruthPointExtractor" "$i")"
done | tee output.txt

The printf outputs the file name and the program output on one line. printf在一行上输出文件名和程序输出。 The aggregated output of all runs within the for-loop is piped to tee , which writes the output to both the named file and standard output. for循环中所有运行的合计输出通过管道传递到tee ,后者将输出写入命名文件和标准输出。

echo does not interpret backslash characters (like \\n) by default. 默认情况下, echo不解释反斜杠字符(例如\\ n)。 Use: 采用:

echo -e $Output


-e     enable interpretation of backslash escapes

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

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