LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   When I use xargs with echo, why does the output contain an extra space? (https://www.linuxquestions.org/questions/linux-general-1/when-i-use-xargs-with-echo-why-does-the-output-contain-an-extra-space-749108/)

web_janitor 08-20-2009 02:52 PM

When I use xargs with echo, why does the output contain an extra space?
 
I have a small puzzler here. I'm trying to create a one-liner, where I am combining the output from two different echo commands. The result is not quite what I expect.

For example, I expect the following command to list both lines aligned. But it does not:

# echo "1 2 3" | xargs echo -e "A B C\n"
A B C
_1 2 3

Notice the extra space on the second line (denoted by the "_"). It seems as if xargs adds a space... but why?

(For others having this problem: In order to fix it, I just add the backspace sequence to echo: echo "1 2 3" | xargs echo -e "A B C\n\b" )

crabboy 08-20-2009 03:06 PM

Consider the example:

echo filename | xargs ls

if xargs did not add a space the command would come out as

lsfilename instead of ls filename

w1k0 08-20-2009 04:18 PM

Try that command:

$ echo "1 2 3" | xargs echo "A B C"
Code:

A B C 1 2 3
Here you see why xargs adds a space:

$ echo -n "1 2 3"; echo "A B C"
Code:

1 2 3A B C


All times are GMT -5. The time now is 04:59 PM.