1

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 
0

3 Answers 3

3

Well looks like

echo "$str" works

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is due to the value of the IFS variable. It is by default set to split words on whitespace characters, which includes the newline character. Using quotation marks prevents word splitting.
This behaviour is documented in the bash manual here: gnu.org/software/bash/manual/bashref.html#Word-Splitting
2

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.

Comments

0

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

echo -e $Output -e enable interpretation of backslash escapes 

2 Comments

-e is not relevant here. Use double-quotes: echo "$Output".
Without quotes, the shell will perform word splitting on the variable's value before invoking echo.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.