Unix – system date into variable unix

dateshunix

I want to run the following script

#!/bin/sh
temp =`date +%Y%m%d`
echo "$temp"

But this not running as expected
it is throwing this error message temp: execute permission denied

Best Solution

You have

temp =`date +%Y%m%d`
    ^

So you need to remove the space between temp and date:

#!/bin/sh
temp=$(date +%Y%m%d) # better than `exp`, thanks @OliverDulac (see comments)
echo "$temp"

With this it works to me.

Make sure the file has execution permissions:

chmod +x your_file

or just execute it with

/bin/sh /your/script/path/your_file