0

I define a variable in my Bash script like:

BIRTHDAY="1993-01-11 11:11 +08:00"

and convert it to epoches use date then echo with echo $(date -d $BIRTHDAY +%s).

However, I get error: date: extra operand ‘+08:00’.

But, it works on a bash interactive shell:

➜  ~ BIRTHDAY="1993-01-11 11:11 +08:00"
➜  ~ date -d $BIRTHDAY +%s
726721860
➜  argos echo $(date -d $BIRTHDAY +%s)
726721860

I alse try to run echo $(date -d \"$BIRTHDAY\" +%s), which not work too.

So, what's wrong with it?

5

You need to quote "$BIRTHDAY", like this:

echo $(date -d "$BIRTHDAY" +%s)

It appears that you are actually using zsh as the interactive shell, as it is the only one in which this command will work correctly (with a default IFS value):

date -d $BIRTHDAY +%s

In any case, even in zsh, quoting the variable is helpful:

date -d "$BIRTHDAY" +%s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.