1

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

jelineau
  • 13
  • 3
  • 1
    Don't use eval if you don't know what you are doing. You can use a subshell `echo "[ $($now) ] my log status"`. But also you shouldn't save commands to variables, so you can use an array instead `now=(date -Iseconds)` and access using `${now[@]}`. – 123 May 08 '17 at 11:22
  • 1
    *Arguments* can be saved in an array; *commands* should be wrapped in a function. – chepner May 08 '17 at 12:05
  • @chepner How come? – 123 May 08 '17 at 12:07
  • Because `"${now[@]}"` is only slightly better than `$now`; it avoids unwanted word-splitting and pathname expansion, but still only works for a small subset of valid commands. It doesn't work with pre-command environment modifiers, redirection, pipes, or compound commands. – chepner May 08 '17 at 12:21

2 Answers2

1

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"
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
0

You can do it all with just echo

echo $(date -Iseconds) my log status;

# 2017-08-16T01:18:25+00:00 my log status
lasec0203
  • 2,422
  • 1
  • 21
  • 36