Subhayan Bhattacharya
Subhayan Bhattacharya

Reputation: 5705

Error with the date command in Bash

I have the below code snippet :

#!/bin/bash
time="date +\"%Y-%m-%d %T,%3N\""
eval echo "`\$time` check"

Actually this is a part of a much larger code . I want to compute the value of time as per the above command on the fly every time the script is run and print the output.

When I run the code this is the below error I get :

date: extra operand ‘%T,%3N"’ Try 'date --help' for more information. check

However when I try to run the command from the command line window it runs fine. The problem is the date has to be in the same format only in the output.

$ date +"%Y-%m-%d %T,%3N"

2017-01-13 11:54:36,604

Please guide me out what should be done in this case.

Thanks in advance for any help

Upvotes: 0

Views: 1649

Answers (2)

codeforester
codeforester

Reputation: 42999

Not sure why you need the indirection of eval and backticks. We could simply write:

time=$(date +"%Y-%m-%d %T,%3N")

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84521

Following from my comment, your problem is that you cannot assign the result of the date command as you have it written, you need:

time=$(date +"%Y-%m-%d %T,%3N")

Upvotes: 1

Related Questions