# echo two command output in the same line



## chuikingman (Jan 25, 2011)

Hi,

I try to write script and echo two command at the same line.


```
echo "A" 
echo "B"
```

How can I pipe above two command at the same line in text file? So, in the output text file, you can see below?


```
A     B
```
not

```
A
B
```
Any sugggestion?


----------



## phoenix (Jan 25, 2011)

Read the man page for echo(1) (or your shell, for the built-in echo command), especially the part about *-n*.


----------



## chuikingman (Jan 25, 2011)

phoenix said:
			
		

> Read the man page for echo(1) (or your shell, for the built-in echo command), especially the part about *-n*.



But actually, I want to put two ps commands together at the same line of the text output file. How can I do that?

Like below 


```
XXXXX            XXXXX
```

where XXXX is output of command ps ....

Any suggestion?


----------



## UNIXgod (Jan 25, 2011)

phoenix said:
			
		

> Read the man page for echo(1) (or your shell, for the built-in echo command), especially the part about *-n*.



I love how the man page tells you to prefer printf =)

`% printf "A\t"; printf "B\n"`

instead of

`% echo -n "A\t"; echo "B"`

printf() is easily more succinct and readable.


----------



## wblock@ (Jan 25, 2011)

UNIXgod said:
			
		

> I love how the man page tells you to prefer printf =)
> 
> `% printf "A\t"; printf "B\n"`
> 
> ...



Besides which, the second won't work with BSD echo, which doesn't understand all the useful escapes.


----------



## DutchDaemon (Jan 25, 2011)

Actually, doesn't [cmd=]printf "$a $b"[/cmd] do what you want?


```
> a=hello
> b=there
> printf "$a $b"
hello there
```


----------



## wblock@ (Jan 25, 2011)

DutchDaemon said:
			
		

> Actually, doesn't [cmd=]printf "$a $b"[/cmd] do what you want?
> 
> 
> ```
> ...



The original post looks like the echo commands are examples that would be replaced by real commands with output.  So we may be into backtick territory here.


```
A=`echo "output from the first command"`
B=`echo "second command output"`
printf "First command: %s\nSecond command:%s\n" "$A" "$B"
```


----------



## phoenix (Jan 25, 2011)

Backticks are so 1990s.  


```
A=$( somecommand )
B=$( someothercommand )
printf "First command: %s\nSecond command:%s\n" "$A" "$B"
```

$() construct is supported by almost every shell out there, and it's definitely supported by /bin/sh (really, the only one you should be scripting in).  It's easier to see and to follow than backticks, which may or may not appear different from single-quotes/apostrophes in every font.

But, yeah, going back to the original question:
You stick your information into variables, then use printf or echo to display the variables, formatted however you want.


----------



## UNIXgod (Jan 25, 2011)

phoenix said:
			
		

> Backticks are so 1990s.
> 
> $() construct is supported by almost every shell out there, and it's definitely supported by /bin/sh (really, the only one you should be scripting in).  It's easier to see and to follow than backticks, which may or may not appear different from single-quotes/apostrophes in every font.



Interesting. I am so used to backticks. I have always considered parens for precedence. Is there any benefit to using parens verses backtick besides readability? Is there any performance benefit?


----------



## dandelion (Jan 26, 2011)

wblock said:
			
		

> UNIXgod said:
> 
> 
> 
> ...


Better check the doc
`$ echo -e "A\t\c"; echo -e "B"`


			
				&quot said:
			
		

> sh[/man]"]*echo* [*-e* | *-n*] [_string_ _..._]
> Print a space-separated list of the arguments to the standard
> output and append a newline character.
> 
> ...


*-e* option is a BSD extension, according to POSIX 2008 escape sequences may be supported without specifying the option.





			
				UNIXgod said:
			
		

> Is there any benefit to using parens verses backtick besides readability?


- less confusing nesting $(...$(...$(...)...)...) vs. `...\`...\\\`...\\\`...\`...`
- more sane quoting, e.g. $(echo -e $(echo -e $(echo -e \\a))) vs. `echo -e \`echo -e \\\`echo -e \\\\\\\\\a\\\`\``


----------



## wblock@ (Jan 26, 2011)

dandelion said:
			
		

> Better check the doc
> `$ echo -e "A\t\c"; echo -e "B"`*-e* option is a BSD extension, according to POSIX 2008 escape sequences may be supported without specifying the option.



If you mean I was not specific enough, I'll modify that:
FreeBSD's echo(1) does not have -e, or support C-style escapes:

```
% echo -e "a\tb"
-e a\tb
```


----------



## phoenix (Jan 26, 2011)

/bin/echo does not support *-e*.

Neither does the echo builtin to /bin/csh.

However, the echo builtin to /bin/sh does.

So, it depends on the shell you are using, and whether or not you use the builtin echo() or the binary echo(1).


----------

