Solved Is there a way to display the result of a command on the same line?

Solved issue

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
775
Reaction score
631
Credits
4,911
For instance, you have an echo message, followed by && and the next command. This usually results in this:
"echo message"
"the result of the next command".

Is there a way to instruct terminal to display these two lines on the same line, like this:

"echo message" "the result of the next command"?
 


Call the command inside the echo statement and use substitution to insert the result into the string output by echo.
E.g.:
Code:
echo -e "Result of command: $(somecommand | \grep somePattern)\n"
The above is an abstract example. We’re running somecommand and piping the results to grep, searching for "somePattern".
The result of that command is substituted into a string that’s output to stdout by the echo command.
 
If you're just targeting echo, it has a -n (no newline argument):

Code:
echo -n 'hello'; echo ' world'

But yes, as already suggested, embedding the output into a string $() or the more older option of back-quotes a more general solution.
Code:
echo "currently at $(pwd) and the date is $(date)"
echo "currently at `pwd` and the date is `date`"

You could also substitute for or delete newlines streaming out of a pipe by using the tr command:
Code:
ls -1 | tr  '\n'  '-'
ps | tr -d '\n'
 
The way of @JasKinasis gave me an idea and I made it work in an even simpler way. Since the command had too many quotes in it and it was gonna become a mess with the echo command, I put it in a script file and then the echo message became like this:

Code:
echo -e "Echo message here:" $(/path/to/the/script.sh)
 
First thing that comes to mind is to use PRINT or PRINTF, as they can print text info without line feed,
but it doesn't matter as " && " will force a line feed anyway, no ?
 
If you're just targeting echo, it has a -n (no newline argument):

Code:
echo -n 'hello'; echo ' world'

But yes, as already suggested, embedding the output into a string $() or the more older option of back-quotes a more general solution.
Code:
echo "currently at $(pwd) and the date is $(date)"
echo "currently at `pwd` and the date is `date`"

You could also substitute for or delete newlines streaming out of a pipe by using the tr command:
Code:
ls -1 | tr  '\n'  '-'
ps | tr -d '\n'
None of these use &&

Isn't that what the threadposter requires to use ?
 
this seems to work just fine, no line feed caused by &&

printf "justa" && printf "test"
 

Members online


Top