How do I suppress or remove a newline at the end of Bash command substitution?
For example, I have
echo "$(python --version) and more text"
how do I get
Python 2.7.10 and more text
and not
Python 2.7.10
and more text
How do I suppress or remove a newline at the end of Bash command substitution?
For example, I have
echo "$(python --version) and more text"
how do I get
Python 2.7.10 and more text
and not
Python 2.7.10
and more text
bash command substitution syntax already removes trailing newlines. All you need to do is to redirect to stdout:
$ echo "$(python --version) and more text"
Python 2.7.8
and more text
$ echo "$(python --version 2>&1) and more text"
Python 2.7.8 and more text
The thing here is that python --version
output to stderr, whereas "and more text"
to stdout.
So the only thing you have to do is redirect stderr to stdin using 2 >&1
:
printf "%s and more text" "$(python --version 2>&1)"
or
$ echo "$(python --version 2>&1) and more text"
Python 2.7.10 and more text
Note that initially I was piping to tr -d '\n'
using |&
:
echo "$(python --version |& tr -d '\n') and more text"