obelix
obelix

Reputation: 900

new line character in bash

I am currently writing a bash scipt where i need to concatenate the results within the output variable. However I need them to be seperated by newline charcater '\n' which does not seem to work... Any ideas ???

#!/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

Upvotes: 1

Views: 4834

Answers (3)

chepner
chepner

Reputation: 532238

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. 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.

Upvotes: 2

obelix
obelix

Reputation: 900

Well looks like

echo "$str" works

because when you print the string without quotes, newline are converted to spaces !!!

Upvotes: 3

phil
phil

Reputation: 115

echo does not interpret backslash characters (like \n) by default. Use:

echo -e $Output


-e     enable interpretation of backslash escapes

Upvotes: 0

Related Questions