shell - Command not found error in Bash variable assignment -


i have script called test.sh:

#!/bin/bash str = "hello world" echo $str 

when run sh test.sh this:

test.sh: line 2: str: command not found 

what doing wrong? @ extremely basic/beginners bash scripting tutorials online , how declare variables... i'm not sure i'm doing wrong.

i'm on ubuntu server 9.10. , yes, bash located @ /bin/bash.

you cannot have spaces around '=' sign.

when write:

str = "foo" 

bash tries run command named str 2 arguments (the strings '=' , 'foo')

when write:

str =foo 

bash tries run command named str 1 argument (the string '=foo')

when write:

str= foo 

bash tries run command foo str set empty string in environment.

i'm not sure if helps clarify or if mere obfuscation, note that:

  1. the first command equivalent to: str "=" "foo",
  2. the second same str "=foo",
  3. and last equivalent str="" foo.

the relevant section of sh language spec, section 2.9.1 states:

a "simple command" sequence of optional variable assignments , redirections, in sequence, optionally followed words , redirections, terminated control operator.

in context, word command bash going run. string containing = (in position other @ beginning of string) not redirection variable assignment, while string not redirection , not contain = command. in str = "foo", str not variable assignment.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -