Posted: . At: 8:16 AM. This was 10 years ago. Post ID: 6602
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

How to add the output of a command when echoing text with bash.

This is how to do this; just add the backticks and you may then echo the output of the logname command in this echo string.

echo "Hello `logname`, this is a nice day is it not?"

This will add the output of the command by executing it and showing the output.

Here we are showing the content of an environment variable.

homer@lollinux-machina:~$ echo "Hello ${LOGNAME}, this is a nice day is it not?"
Hello homer, this is a nice day is it not?

This is the environment variable value in capitals.

homer@lollinux-machina:~$ echo "Hello ${LOGNAME^^}, this is a nice day is it not?"
Hello HOMER, this is a nice day is it not?

And with the first letter capitalized.

homer@lollinux-machina:~$ echo "Hello ${LOGNAME^}, this is a nice day is it not?"
Hello Homer, this is a nice day is it not?

This command is printing the kernel version.

homer@lollinux-machina:~$ echo "This is the kernel release: `uname -r`"
This is the kernel release: 3.12.0-3-generic

These tips are very useful when you are writing a shell script. See what you can do with this!

1 thought on “How to add the output of a command when echoing text with bash.”

Leave a Reply to Alireza Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.