23

I want to store date values into a variable for future use. Please correct the sample script:

#!/bin/bash
Now_hourly = $(date +%d-%b-%H_%M)    
Now_daily = $(date +%d-%b-daily)    
echo $(Now_hourly)    
echo $(Now_daily)

The output should be : 12-Feb-17_50 and 12-Feb-daily
But When I run the script, I am getting below error :
Now_hourly: command not found
Now_daily: command not found

4
  • 5
    It's a FAQ. You can't have spaces on either side of = while assigning a variable. Commented Feb 12, 2014 at 7:59
  • 1
    This question appears to be off-topic because the OP expects the reader to correct the sample script. Commented Feb 12, 2014 at 8:01
  • @Gopu you can get the correct answer by testing everything that reach to your mind..no problem..It is solved :) Commented Feb 12, 2014 at 8:38
  • Besides: use curly braces in the echo lines, otherwise Now_hourly will be tried to execute. Commented Feb 12, 2014 at 8:41

1 Answer 1

52

you can change the script like:

#!/bin/bash
Now_hourly=$(date +%d-%b-%H_%M)    
Now_daily=$(date +%d-%b-daily)    
echo "$Now_hourly"
echo "$Now_daily"

I think the problem is spaces around =

output:

12-Feb-12_03
12-Feb-daily
Sign up to request clarification or add additional context in comments.

1 Comment

Also, as a generic answer, if your date format may contain spaces, you should keep the format in double quotes, like so --> Now_hourly=$(date +"%d-%b-%H_%M")

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.