Command not found error in Bash variable assignment












440















I have this script called 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




















  • 30





    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






  • 3





    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 '16 at 16:49






  • 3





    These days (almost seven years later!) there's a FOSS linter/analyzer called shellcheck that will autodetect this and other common syntax issues. It can be used online or installed offline and integrated in your editor.

    – that other guy
    Nov 8 '16 at 17:52











  • See also stackoverflow.com/questions/26971987/…

    – tripleee
    Oct 2 '17 at 13:36











  • I'd recommend you to use: #!/usr/bin/env bash instead of putting directly #!/bin/bash unless you're absolutely sure your bash is in /bin because of this answer: stackoverflow.com/a/21613044/3589567

    – Alejandro Blasco
    Feb 21 '18 at 10:58
















440















I have this script called 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




















  • 30





    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






  • 3





    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 '16 at 16:49






  • 3





    These days (almost seven years later!) there's a FOSS linter/analyzer called shellcheck that will autodetect this and other common syntax issues. It can be used online or installed offline and integrated in your editor.

    – that other guy
    Nov 8 '16 at 17:52











  • See also stackoverflow.com/questions/26971987/…

    – tripleee
    Oct 2 '17 at 13:36











  • I'd recommend you to use: #!/usr/bin/env bash instead of putting directly #!/bin/bash unless you're absolutely sure your bash is in /bin because of this answer: stackoverflow.com/a/21613044/3589567

    – Alejandro Blasco
    Feb 21 '18 at 10:58














440












440








440


92






I have this script called 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
















I have this script called 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.







bash shell syntax sh variable-assignment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 8 '18 at 21:01









codeforester

18.4k84267




18.4k84267










asked Feb 15 '10 at 18:32









Jake WilsonJake Wilson

41.3k66198305




41.3k66198305








  • 30





    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






  • 3





    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 '16 at 16:49






  • 3





    These days (almost seven years later!) there's a FOSS linter/analyzer called shellcheck that will autodetect this and other common syntax issues. It can be used online or installed offline and integrated in your editor.

    – that other guy
    Nov 8 '16 at 17:52











  • See also stackoverflow.com/questions/26971987/…

    – tripleee
    Oct 2 '17 at 13:36











  • I'd recommend you to use: #!/usr/bin/env bash instead of putting directly #!/bin/bash unless you're absolutely sure your bash is in /bin because of this answer: stackoverflow.com/a/21613044/3589567

    – Alejandro Blasco
    Feb 21 '18 at 10:58














  • 30





    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






  • 3





    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 '16 at 16:49






  • 3





    These days (almost seven years later!) there's a FOSS linter/analyzer called shellcheck that will autodetect this and other common syntax issues. It can be used online or installed offline and integrated in your editor.

    – that other guy
    Nov 8 '16 at 17:52











  • See also stackoverflow.com/questions/26971987/…

    – tripleee
    Oct 2 '17 at 13:36











  • I'd recommend you to use: #!/usr/bin/env bash instead of putting directly #!/bin/bash unless you're absolutely sure your bash is in /bin because of this answer: stackoverflow.com/a/21613044/3589567

    – Alejandro Blasco
    Feb 21 '18 at 10:58








30




30





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





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




3




3





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 '16 at 16:49





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 '16 at 16:49




3




3





These days (almost seven years later!) there's a FOSS linter/analyzer called shellcheck that will autodetect this and other common syntax issues. It can be used online or installed offline and integrated in your editor.

– that other guy
Nov 8 '16 at 17:52





These days (almost seven years later!) there's a FOSS linter/analyzer called shellcheck that will autodetect this and other common syntax issues. It can be used online or installed offline and integrated in your editor.

– that other guy
Nov 8 '16 at 17:52













See also stackoverflow.com/questions/26971987/…

– tripleee
Oct 2 '17 at 13:36





See also stackoverflow.com/questions/26971987/…

– tripleee
Oct 2 '17 at 13:36













I'd recommend you to use: #!/usr/bin/env bash instead of putting directly #!/bin/bash unless you're absolutely sure your bash is in /bin because of this answer: stackoverflow.com/a/21613044/3589567

– Alejandro Blasco
Feb 21 '18 at 10:58





I'd recommend you to use: #!/usr/bin/env bash instead of putting directly #!/bin/bash unless you're absolutely sure your bash is in /bin because of this answer: stackoverflow.com/a/21613044/3589567

– Alejandro Blasco
Feb 21 '18 at 10:58












5 Answers
5






active

oldest

votes


















812





+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 '16 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 '16 at 13:30






  • 1





    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 '16 at 11:54






  • 1





    @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 '16 at 21:29



















147














Drop the spaces around the = sign:



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





share|improve this answer



















  • 7





    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











  • Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

    – Freedo
    Jan 19 '18 at 16:51



















5














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





















  • 2





    But note the OP was saying STR = "Hello World", so this answer does not apply here.

    – fedorqui
    Aug 16 '16 at 12:05











  • @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

    – Kasun Siyambalapitiya
    Nov 22 '16 at 13:24











  • @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

    – numbermaniac
    Jan 5 '17 at 23:59



















0














When you define any variable then you do not have to put in any extra spaces.



E.g.



name = "Stack Overflow"  
// it is not valid, you will get an error saying- "Command not found"


So remove spaces:



name="Stack Overflow" 


and it will work fine.






share|improve this answer

































    -1














    I have exact the same problem but it seems not to be an syntax error. Here ist a simple script:



    #!/bin/bash
    ssh -T user@server <<-EOSSH
    VAR1=test
    cd $VAR1
    EOSSH


    this outputs following errors where I believe the first two are negotiable:



    Warning: no access to tty (Bad file number).
    Thus no job control in this shell.
    VAR1=test: Command not found.





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2268104%2fcommand-not-found-error-in-bash-variable-assignment%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      812





      +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 '16 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 '16 at 13:30






      • 1





        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 '16 at 11:54






      • 1





        @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 '16 at 21:29
















      812





      +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 '16 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 '16 at 13:30






      • 1





        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 '16 at 11:54






      • 1





        @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 '16 at 21:29














      812





      +100







      812





      +100



      812




      +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















      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














      share|improve this answer



      share|improve this answer








      edited Aug 17 '16 at 7:49









      fedorqui

      170k54348389




      170k54348389










      answered Feb 15 '10 at 18:34









      William PursellWilliam Pursell

      132k32206239




      132k32206239













      • 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 '16 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 '16 at 13:30






      • 1





        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 '16 at 11:54






      • 1





        @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 '16 at 21:29



















      • 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 '16 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 '16 at 13:30






      • 1





        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 '16 at 11:54






      • 1





        @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 '16 at 21:29

















      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 '16 at 2:27





      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 '16 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 '16 at 13:30





      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 '16 at 13:30




      1




      1





      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 '16 at 11:54





      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 '16 at 11:54




      1




      1





      @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 '16 at 21:29





      @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 '16 at 21:29













      147














      Drop the spaces around the = sign:



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





      share|improve this answer



















      • 7





        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











      • Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

        – Freedo
        Jan 19 '18 at 16:51
















      147














      Drop the spaces around the = sign:



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





      share|improve this answer



















      • 7





        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











      • Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

        – Freedo
        Jan 19 '18 at 16:51














      147












      147








      147







      Drop the spaces around the = sign:



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





      share|improve this answer













      Drop the spaces around the = sign:



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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Feb 15 '10 at 18:34









      JoeyJoey

      266k64568601




      266k64568601








      • 7





        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











      • Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

        – Freedo
        Jan 19 '18 at 16:51














      • 7





        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











      • Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

        – Freedo
        Jan 19 '18 at 16:51








      7




      7





      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





      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





      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













      Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

      – Freedo
      Jan 19 '18 at 16:51





      Why bash doesn't accept numbers in the left field? like 3="Hello World", it complains about command not found

      – Freedo
      Jan 19 '18 at 16:51











      5














      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





















      • 2





        But note the OP was saying STR = "Hello World", so this answer does not apply here.

        – fedorqui
        Aug 16 '16 at 12:05











      • @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

        – Kasun Siyambalapitiya
        Nov 22 '16 at 13:24











      • @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

        – numbermaniac
        Jan 5 '17 at 23:59
















      5














      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





















      • 2





        But note the OP was saying STR = "Hello World", so this answer does not apply here.

        – fedorqui
        Aug 16 '16 at 12:05











      • @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

        – Kasun Siyambalapitiya
        Nov 22 '16 at 13:24











      • @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

        – numbermaniac
        Jan 5 '17 at 23:59














      5












      5








      5







      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















      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














      share|improve this answer



      share|improve this answer








      edited Aug 16 '16 at 12:05









      fedorqui

      170k54348389




      170k54348389










      answered Feb 26 '10 at 6:33









      ArkapravoArkapravo

      2,58693245




      2,58693245








      • 2





        But note the OP was saying STR = "Hello World", so this answer does not apply here.

        – fedorqui
        Aug 16 '16 at 12:05











      • @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

        – Kasun Siyambalapitiya
        Nov 22 '16 at 13:24











      • @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

        – numbermaniac
        Jan 5 '17 at 23:59














      • 2





        But note the OP was saying STR = "Hello World", so this answer does not apply here.

        – fedorqui
        Aug 16 '16 at 12:05











      • @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

        – Kasun Siyambalapitiya
        Nov 22 '16 at 13:24











      • @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

        – numbermaniac
        Jan 5 '17 at 23:59








      2




      2





      But note the OP was saying STR = "Hello World", so this answer does not apply here.

      – fedorqui
      Aug 16 '16 at 12:05





      But note the OP was saying STR = "Hello World", so this answer does not apply here.

      – fedorqui
      Aug 16 '16 at 12:05













      @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

      – Kasun Siyambalapitiya
      Nov 22 '16 at 13:24





      @Arkapravo what is the meaning of the interactive mode, is it has something to do with the $ mark

      – Kasun Siyambalapitiya
      Nov 22 '16 at 13:24













      @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

      – numbermaniac
      Jan 5 '17 at 23:59





      @KasunSiyambalapitiya by "interactive mode" he means typing those commands in the actual terminal, not into a script.

      – numbermaniac
      Jan 5 '17 at 23:59











      0














      When you define any variable then you do not have to put in any extra spaces.



      E.g.



      name = "Stack Overflow"  
      // it is not valid, you will get an error saying- "Command not found"


      So remove spaces:



      name="Stack Overflow" 


      and it will work fine.






      share|improve this answer






























        0














        When you define any variable then you do not have to put in any extra spaces.



        E.g.



        name = "Stack Overflow"  
        // it is not valid, you will get an error saying- "Command not found"


        So remove spaces:



        name="Stack Overflow" 


        and it will work fine.






        share|improve this answer




























          0












          0








          0







          When you define any variable then you do not have to put in any extra spaces.



          E.g.



          name = "Stack Overflow"  
          // it is not valid, you will get an error saying- "Command not found"


          So remove spaces:



          name="Stack Overflow" 


          and it will work fine.






          share|improve this answer















          When you define any variable then you do not have to put in any extra spaces.



          E.g.



          name = "Stack Overflow"  
          // it is not valid, you will get an error saying- "Command not found"


          So remove spaces:



          name="Stack Overflow" 


          and it will work fine.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 19 '17 at 16:42









          Christopher Kyle Horton

          665825




          665825










          answered Jun 19 '17 at 15:16









          Ravi SolankiRavi Solanki

          5916




          5916























              -1














              I have exact the same problem but it seems not to be an syntax error. Here ist a simple script:



              #!/bin/bash
              ssh -T user@server <<-EOSSH
              VAR1=test
              cd $VAR1
              EOSSH


              this outputs following errors where I believe the first two are negotiable:



              Warning: no access to tty (Bad file number).
              Thus no job control in this shell.
              VAR1=test: Command not found.





              share|improve this answer




























                -1














                I have exact the same problem but it seems not to be an syntax error. Here ist a simple script:



                #!/bin/bash
                ssh -T user@server <<-EOSSH
                VAR1=test
                cd $VAR1
                EOSSH


                this outputs following errors where I believe the first two are negotiable:



                Warning: no access to tty (Bad file number).
                Thus no job control in this shell.
                VAR1=test: Command not found.





                share|improve this answer


























                  -1












                  -1








                  -1







                  I have exact the same problem but it seems not to be an syntax error. Here ist a simple script:



                  #!/bin/bash
                  ssh -T user@server <<-EOSSH
                  VAR1=test
                  cd $VAR1
                  EOSSH


                  this outputs following errors where I believe the first two are negotiable:



                  Warning: no access to tty (Bad file number).
                  Thus no job control in this shell.
                  VAR1=test: Command not found.





                  share|improve this answer













                  I have exact the same problem but it seems not to be an syntax error. Here ist a simple script:



                  #!/bin/bash
                  ssh -T user@server <<-EOSSH
                  VAR1=test
                  cd $VAR1
                  EOSSH


                  this outputs following errors where I believe the first two are negotiable:



                  Warning: no access to tty (Bad file number).
                  Thus no job control in this shell.
                  VAR1=test: Command not found.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 1 at 11:05









                  mokoranamokorana

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2268104%2fcommand-not-found-error-in-bash-variable-assignment%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno