0

-bash-4.1# cat output
"/root/mail/domain.com/root/Archive Dir.2012.04 April"

for i in `cat output`; do echo "$i"; done
"/root/mail/domain.com/root/Archive
Dir.2012.04
April"

I want them on a single line, as they are originally stored in the file. Is it echo's fault?
echo -n will break everything, so it won't do.

2 Answers 2

3

You can use a while loop

while read i do echo $i done < output 

The reason why your command doesn't work as you expect is that for operates on words. The value of the $IFS variable determines what characters are used to delimit words, the default is space, tab and newline. As your input file contains lines that have spaces they are being spit into words. If you need to use a for loop you can work around this by wraping your lines in "

for i in "`cat output`" do echo "$i" done 
3
  • hmpf, same error when replacing echo with mkdir, in both for and while. Commented Nov 21, 2012 at 11:10
  • @w00t: Yes and it will be for exactly the same reason. I'll leave it as an exercise for you to figure it our yourself. This is all very basic stuff you should invest some time in learning the tools you're working with rather than blundering from one problem to the next. It'll save you lots of time in the long run. Commented Nov 21, 2012 at 11:57
  • it works with echo, why shouldn't it work with any other command? Again, it does the same even when using while Commented Nov 22, 2012 at 12:18
2
while read line; do echo $line; done < output 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.