echo two command output in the same line

Hi,

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

Code:
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?

Code:
A     B
not
Code:
A
B
Any sugggestion?
 
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

Code:
 XXXXX            XXXXX

where XXXX is output of command ps ....

Any suggestion?
 
UNIXgod said:
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.

Besides which, the second won't work with BSD echo, which doesn't understand all the useful escapes.
 
Actually, doesn't [cmd=]printf "$a $b"[/cmd] do what you want?

Code:
> a=hello
> b=there
> printf "$a $b"
hello there
 
DutchDaemon said:
Actually, doesn't [cmd=]printf "$a $b"[/cmd] do what you want?

Code:
> a=hello
> b=there
> printf "$a $b"
hello there

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.

Code:
A=`echo "output from the first command"`
B=`echo "second command output"`
printf "First command: %s\nSecond command:%s\n" "$A" "$B"
 
Backticks are so 1990s. ;)

Code:
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.
 
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?
 
wblock said:
UNIXgod said:
% echo -n "A\t"; echo "B"
won't work with BSD echo, which doesn't understand all the useful escapes.
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.

-n Suppress the output of the trailing newline.

-e Process C-style backslash escape sequences. The echo
command understands the following character escapes:
[...]
\t Horizontal tab
-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\\\`\``
 
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:
Code:
% echo -e "a\tb"
-e a\tb
 
/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).
 
Back
Top