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

Time:01-11

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

CodePudding user response:

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"

CodePudding user response:

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."
  • Related