0

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

2
  • get rid of the '\''s and the extra quotes. time=$(date +"%Y-%m-%d %T,%3N") Commented Jan 13, 2017 at 6:29
  • don;t use eval, without knowing its consequences. Commented Jan 13, 2017 at 6:33

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

Thank you codeforester . I tried your way as well as tried a different way with eval . Both worked :echo "$(date +"%Y-%m-%d %T,%3N") check----" and #!/bin/bash dt="date +\"%Y-%m-%d\"" time="date +\"%T,%3N\"" eval echo "\$dt \$time check"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.