1

I am trying to set a variable with the current date but having a tab between the date and the time. On the current variable I want also the time to be 1 hour back.

This works fine:

#date -d '1 hour ago' "+%Y-%m-%d"$'\t'"%H:"
2019-07-17      08:

But when I'm trying to set it to a variable the tab is replaced by space:

#var1=$(date -d '1 hour ago' "+%Y-%m-%d"$'\t'"%H:")
#echo $var1
2019-07-17 08:

#var1=`date -d '1 hour ago' "+%Y-%m-%d"$'\t'"%H:"`
#echo $var1
2019-07-17 08:

Any idea why this is happening and how i can include the tab in the variable?

1 Answers1

3

The tab is in the variable. You're destroying it by printing it wrong.

echo "$var1"
# => 2019-07-17      08:

If you say just echo $var1, then echo gets two parameters separated by whitespace (your tab); echo prints each parameter separated by a space.

If you say echo "$var1", then echo gets a single parameter, with tab included.

Amadan
  • 191,408
  • 23
  • 240
  • 301