0
OS: Ubuntu 14.04

I created a test file called test and in it I have:

#!/bin/bash
NEW_VALUE = "/home/"
echo $NEW_VALUE

chmod +x test

./test

Produces the following:

./test: line 2: NEW_VALUE: command not found

Any ideas why this is not working?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • 1
    There is NO SPACE ALLOWED in the assignment. e.g `NEW_VALUE="/home/"` should work. – David C. Rankin Dec 29 '15 at 02:07
  • there is a valid shell command named `test`. Avoid a whole class of problems by using a distinct name like `my_test`. Good luck. – shellter Dec 29 '15 at 02:47
  • Incidentally, http://shellcheck.net/ would have caught this [and the other -- quoting-related -- bug in your script] for you without needing to get humans involved; it's also covered in the bash tag wiki at http://stackoverflow.com/tags/bash/info. – Charles Duffy Dec 29 '15 at 03:08
  • Also, all-caps variable names are reserved for variables that change behavior of the system or the shell [or are provided by either of the above]; your own variables should have lowercase names. See the fourth paragraph of http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html, keeping in mind that environment variables and shell variables share a namespace. – Charles Duffy Dec 29 '15 at 03:10

2 Answers2

2

In Bash you can't have any space around the = sign when assigning a variable. Any space will end the assignment, even after the =, e.g.:

test_var=this is bad
#=> is: command not found

@CharlesDuffy's comment explaining why this happens

Check this link for more information on variable assignment in bash: http://wiki.bash-hackers.org/scripting/newbie_traps#setting_variables

Community
  • 1
  • 1
lleaff
  • 4,249
  • 17
  • 23
  • Please don't link the ABS -- unlike the better references (bash-hackers, Wooledge wiki), it makes no effort to avoid showcasing bad practices in its examples; half of what we do in the freenode #bash channel is helping people unlearn bad habits they picked up there. – Charles Duffy Dec 29 '15 at 03:06
  • @CharlesDuffy Ok, even if it's the first result on Google what you say is totally plausible given that the same thing is happening with web development and w3school. Changed to bash-hackers.org. – lleaff Dec 29 '15 at 03:10
  • 1
    ...that said, the behavior given isn't quite right for `test_var=this is bad`; that command would be running the command `is` with the argument `bad` with the environment variable `test_var` set to `this` for only the scope of that single command (so `test_var` would be unset for future commands, rather than containing `this`). – Charles Duffy Dec 29 '15 at 03:12
  • True true, you should just edit my answer directly ;) I realize my bash instinct has faded somewhat. – lleaff Dec 29 '15 at 03:23
0

Remove the white space while assigning then it'll work fine. just like:

#!/bin/bash
NEW_VALUE="/home/"
echo $NEW_VALUE
Hunter Zhao
  • 4,589
  • 2
  • 25
  • 39