4

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

fedorqui
  • 275,237
  • 103
  • 548
  • 598
BKK
  • 1,199
  • 6
  • 16
  • 24
  • Possible duplicate of [Bash script variable declaration - command not found](http://stackoverflow.com/questions/2268104/bash-script-variable-declaration-command-not-found) – tripleee Dec 28 '16 at 11:21

5 Answers5

9

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • And, as a good habit (and to allow easy nesting), use '$(...)' instead : `temp=$(date +%Y%m%d)` . Inside '$(...)', you can use quotes and variables and special characters *at that level*, ie, as if you were writing the inside on a separate line. ex: `foo=$(echo "$var $(echo $"var")")` – Olivier Dulac May 27 '13 at 10:08
  • Good point, @OlivierDulac! It is always good to remember that, so I update my answer with this. Thanks – fedorqui May 27 '13 at 10:12
  • You should remove the chmod +x part : "permission denied" comes because, with an extra space, bash sees: `temp =20130527` (if today is the 27th in the OP's locale. ie : command/alias/function `temp`, followed by argument `=20130527`), and so it tries to execute "temp". It so appear that there is a "temp" file in the PATH (maybe, heaven forbid, in the current directory, if the PATH contains ".", which it should NOT). Giving that file/dir execute rights will not do what the OP wants to do (ie, define the "temp" variable, not execute a file with same name) – Olivier Dulac May 27 '13 at 10:22
0

temp variable needs to be assigned without spaces.

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
0

Your script indeed has a syntax error, to fix it remove the space after temp in the second line, but this error will not throw execute permission denied but line 2: temp: command not found.

Your script does not have the execution rights, to fix it invoke:

chmod +x FILE.sh

where FILE is the name of this script.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
0

In sh scripts (not just bash) the equal sign is not accepted when assigning a variable, because you could do something like this:

VARIABLE=something ./runcommand

to have that variable exported to the ./runcommand subprocess but not in general exported to all subprocesses.

If space were allowed there would be no way to distinguish the end of the assignment with the start of the command line.

LtWorf
  • 7,286
  • 6
  • 31
  • 45
0

I found the mistake in my above code, i allowed a space after the string temp. that is causing the error.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
BKK
  • 1,199
  • 6
  • 16
  • 24