Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This seems like such a simple question I'm embarrassed to ask it:

test.sh

#!/bin/bash
STR = "Hello World"
echo $STR

when I run sh test.sh I get this:

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

What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and this is how they say to declare variables... So I'm not sure what I'm doing wrong.

I'm on Ubuntu Server 9.10. And yes, bash is located at /bin/bash.

share|improve this question
7  
I'm glad you did ask the question, you're not the only bash noob out there! – miller the gorilla Aug 26 '15 at 10:40
2  
Thanks for asking that question. This is not a question to be embarrassed about. I am working late night in office & there is no Bash expert around me to answer this. – Adway Lele Jan 19 at 16:49
up vote 511 down vote accepted
+100

You cannot have spaces around your '=' sign.

When you write:

STR = "foo"

bash tries to run a command named STR with 2 arguments (the strings '=' and 'foo')

When you write:

STR =foo

bash tries to run a command named STR with 1 argument (the string '=foo')

When you write:

STR= foo

bash tries to run the command foo with STR set to the empty string in its environment.

I'm not sure if this helps to clarify or if it is mere obfuscation, but note that:

  1. the first command is exactly equivalent to: STR "=" "foo",
  2. the second is the same as STR "=foo",
  3. and the last is equivalent to STR="" foo.

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

A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.

In that context, a word is the command that bash is going to run. Any string containing = (in any position other than at the beginning of the string) which is not a redirection is a variable assignment, while any string that is not a redirection and does not contain = is a command. In STR = "foo", STR is not a variable assignment.

share|improve this answer
    
If you have a variable with a name that contaings "-", the same error happens. In that case, the solution is to remove the "-" – chomp Jun 17 at 2:27
    
chomp@ In rule 7b of section 2.10.10 of pubs.opengroup.org/onlinepubs/9699919799 "If all the characters preceding '=' form a valid name (see XBD Name), the token ASSIGNMENT_WORD shall be returned." Following the link to section 3.231 of pubs.opengroup.org/onlinepubs/9699919799, we find "In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit.". So the word FOO-BAR=qux is not a variable assignment since FOO-BAR is not a valid name. – William Pursell Jun 17 at 13:30
    
I am offering a bounty to reward this clear explanation to an always common issue for beginners (well, and also to be able to find it faster whenever I want to link it :D). Thanks for making things this easy! – fedorqui Aug 16 at 11:54
    
@fedorqui Thanks! I'm not entirely convinced that this is a clear explanation, and I often wonder if it could be made simpler. – William Pursell Aug 16 at 21:29

Drop the spaces around the = sign:

#!/bin/bash 
STR="Hello World" 
echo $STR 
share|improve this answer
6  
This is funny, though, as set foo = bar is a common mistake in Windows batch files as well—and there the batch language is ridiculed for it ;-) – Joey Feb 15 '10 at 18:36
    
Thanks @joey. I was stuck in writing a shell script where i was initializing variables with spaces after "=". You saved my day – Lalit Rao Oct 2 '15 at 13:10

In the interactive mode everything looks fine

$ str="Hello World"
$ echo $str
Hello World

Obviously ! as Johannes said, no space around '='. In case there is any space around '=' then in the interactive mode it gives errors as `

No command 'str' found

share|improve this answer
1  
But note the OP was saying STR = "Hello World", so this answer does not apply here. – fedorqui Aug 16 at 12:05

I have read all answers, but there is still one thing to note:

STR= hello
STR=hello ls

The 1st line will run command hello, but will NOT set STR to "".

The 2nd line will run command ls but will NOT set STR to "hello".

Let use echo $STR for each step to see the result.

share|improve this answer
5  
Not strictly true. The first command does not modify the value of STR in the current shell, but it does set it (to the empty string) in the environment of hello. Similarly, the second command does set STR (to the string 'hello') in the environment of ls. (But ls ignores the value.) – William Pursell Apr 9 '15 at 21:00
1  
Also, you want echo "$STR" with proper quoting to avoid having the shell replacing parts or all of the value. And, don't use uppercase variable names. – tripleee Jun 27 at 12:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.