jelineau
jelineau

Reputation: 13

Concat string and eval

I'am trying to output an inline status with date

now="date -Iseconds"
echo "[" eval $now echo "] my log status"

this returning line

now="date -Iseconds"
echo "["; eval $now; echo "] my log status"

I don't whant to do

now=`date -Iseconds`

Because time clock between 2 logs status and it store the time when the variable is initialized

Upvotes: 1

Views: 2386

Answers (2)

lasec0203
lasec0203

Reputation: 2683

You can do it all with just echo

echo $(date -Iseconds) my log status;

# 2017-08-16T01:18:25+00:00 my log status

Upvotes: 0

gniourf_gniourf
gniourf_gniourf

Reputation: 46903

Use a function:

now() { date -Iseconds; }

echo "[$(now)] my log status"

Even better, you can use a function that will output your message, with the date prepended:

msg_now() { printf '[%s] %s\n' "$(date -Iseconds)" "$*"; }

and use as:

msg_now "my log status"

Upvotes: 1

Related Questions