简体   繁体   中英

Bash: How to assign echo'ed sentense to a variable in the same line

I tried searching existing questions but could not find one suites to my need.

I am trying to add a line to logs and the same has to be assigned to a variable QSubject so that, I can send an email with this as subject.

Is there a way to achieve the same in one line?

I tried this but learned that, it will only work if QSubject is a file.

echo "`date` No client_data records have been updated on `hostname` No Records to commit." > QSubject
toto=$(echo "`date` No client_data records have been updated on `hostname` No Records to commit.")
echo $toto

Output:

mar. 10 janv. 2023 16:27:03 No client_data records have been updated on CH15019 No Records to commit.

Or as @jhnc pointed out, even shorter:

toto="`date` No client_data records have been updated on `hostname` No Records to commit."

If you know that all the output goes to stdout, you could pipe to tee and also print to stderr, which would not be captured by the command substitution:

$ var=$(echo "something" | tee /dev/stderr)
something
$ declare -p var
declare -- var="something"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM