bash: Bad Substitution











up vote
85
down vote

favorite
20












#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}


This bash script gives me Bad substitution error on Ubuntu. Any help will be highly appreciated.










share|improve this question
























  • It is working fine to me. What are you trying to accomplish?
    – fedorqui
    Dec 16 '13 at 16:03










  • I am trying to divide the jobname into two: job_201312161447 and 0003. Its giving this error only when I am trying to run this on ubuntu.
    – Arindam Choudhury
    Dec 16 '13 at 16:04










  • Mmmm strange. What if you use cut? cut -d_ -f1,2 <<< "$jobname" and cut -d_ -f3 <<< "$jobname" make it
    – fedorqui
    Dec 16 '13 at 16:06










  • thanks. but why jobname_pre=${jobname:0:16} gave error
    – Arindam Choudhury
    Dec 16 '13 at 16:15






  • 1




    @bludger you are right, I see that if you do sh script.sh it gets a "Bad substitution" error.
    – fedorqui
    Dec 16 '13 at 16:18















up vote
85
down vote

favorite
20












#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}


This bash script gives me Bad substitution error on Ubuntu. Any help will be highly appreciated.










share|improve this question
























  • It is working fine to me. What are you trying to accomplish?
    – fedorqui
    Dec 16 '13 at 16:03










  • I am trying to divide the jobname into two: job_201312161447 and 0003. Its giving this error only when I am trying to run this on ubuntu.
    – Arindam Choudhury
    Dec 16 '13 at 16:04










  • Mmmm strange. What if you use cut? cut -d_ -f1,2 <<< "$jobname" and cut -d_ -f3 <<< "$jobname" make it
    – fedorqui
    Dec 16 '13 at 16:06










  • thanks. but why jobname_pre=${jobname:0:16} gave error
    – Arindam Choudhury
    Dec 16 '13 at 16:15






  • 1




    @bludger you are right, I see that if you do sh script.sh it gets a "Bad substitution" error.
    – fedorqui
    Dec 16 '13 at 16:18













up vote
85
down vote

favorite
20









up vote
85
down vote

favorite
20






20





#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}


This bash script gives me Bad substitution error on Ubuntu. Any help will be highly appreciated.










share|improve this question















#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}


This bash script gives me Bad substitution error on Ubuntu. Any help will be highly appreciated.







string bash ubuntu substitution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 8 at 14:50

























asked Dec 16 '13 at 16:01









Arindam Choudhury

6221716




6221716












  • It is working fine to me. What are you trying to accomplish?
    – fedorqui
    Dec 16 '13 at 16:03










  • I am trying to divide the jobname into two: job_201312161447 and 0003. Its giving this error only when I am trying to run this on ubuntu.
    – Arindam Choudhury
    Dec 16 '13 at 16:04










  • Mmmm strange. What if you use cut? cut -d_ -f1,2 <<< "$jobname" and cut -d_ -f3 <<< "$jobname" make it
    – fedorqui
    Dec 16 '13 at 16:06










  • thanks. but why jobname_pre=${jobname:0:16} gave error
    – Arindam Choudhury
    Dec 16 '13 at 16:15






  • 1




    @bludger you are right, I see that if you do sh script.sh it gets a "Bad substitution" error.
    – fedorqui
    Dec 16 '13 at 16:18


















  • It is working fine to me. What are you trying to accomplish?
    – fedorqui
    Dec 16 '13 at 16:03










  • I am trying to divide the jobname into two: job_201312161447 and 0003. Its giving this error only when I am trying to run this on ubuntu.
    – Arindam Choudhury
    Dec 16 '13 at 16:04










  • Mmmm strange. What if you use cut? cut -d_ -f1,2 <<< "$jobname" and cut -d_ -f3 <<< "$jobname" make it
    – fedorqui
    Dec 16 '13 at 16:06










  • thanks. but why jobname_pre=${jobname:0:16} gave error
    – Arindam Choudhury
    Dec 16 '13 at 16:15






  • 1




    @bludger you are right, I see that if you do sh script.sh it gets a "Bad substitution" error.
    – fedorqui
    Dec 16 '13 at 16:18
















It is working fine to me. What are you trying to accomplish?
– fedorqui
Dec 16 '13 at 16:03




It is working fine to me. What are you trying to accomplish?
– fedorqui
Dec 16 '13 at 16:03












I am trying to divide the jobname into two: job_201312161447 and 0003. Its giving this error only when I am trying to run this on ubuntu.
– Arindam Choudhury
Dec 16 '13 at 16:04




I am trying to divide the jobname into two: job_201312161447 and 0003. Its giving this error only when I am trying to run this on ubuntu.
– Arindam Choudhury
Dec 16 '13 at 16:04












Mmmm strange. What if you use cut? cut -d_ -f1,2 <<< "$jobname" and cut -d_ -f3 <<< "$jobname" make it
– fedorqui
Dec 16 '13 at 16:06




Mmmm strange. What if you use cut? cut -d_ -f1,2 <<< "$jobname" and cut -d_ -f3 <<< "$jobname" make it
– fedorqui
Dec 16 '13 at 16:06












thanks. but why jobname_pre=${jobname:0:16} gave error
– Arindam Choudhury
Dec 16 '13 at 16:15




thanks. but why jobname_pre=${jobname:0:16} gave error
– Arindam Choudhury
Dec 16 '13 at 16:15




1




1




@bludger you are right, I see that if you do sh script.sh it gets a "Bad substitution" error.
– fedorqui
Dec 16 '13 at 16:18




@bludger you are right, I see that if you do sh script.sh it gets a "Bad substitution" error.
– fedorqui
Dec 16 '13 at 16:18












7 Answers
7






active

oldest

votes

















up vote
136
down vote



accepted










The default shell (/bin/sh) under Ubuntu points to dash, not bash.



me@pc:~$ readlink -f $(which sh)
/bin/dash


So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.



Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.






share|improve this answer

















  • 1




    He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
    – DanFromGermany
    Dec 1 '15 at 10:31








  • 2




    @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
    – Vanni Totaro
    Dec 1 '15 at 11:10








  • 1




    I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
    – DanFromGermany
    Dec 1 '15 at 12:47






  • 2




    @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
    – Vanni Totaro
    Dec 1 '15 at 13:27






  • 1




    Change #!/bin/sh to #!/bin/bash work to me.
    – Evi Song
    Oct 23 at 6:18


















up vote
43
down vote













I had the same problem. Make sure your script didnt have



#!/bin/sh 


at the top of your script. Instead, you should add



#!/bin/bash





share|improve this answer



















  • 4




    I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
    – whyisyoung
    Apr 9 '15 at 2:13












  • If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
    – Jamie
    Aug 20 '16 at 9:56






  • 1




    @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
    – user13107
    Sep 7 '16 at 1:13






  • 1




    @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
    – bfontaine
    Oct 4 at 14:21


















up vote
18
down vote













Your script syntax is valid bash and good.



Possible causes for the failure:




  1. Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
    Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.


  2. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.







share|improve this answer

















  • 7




    I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
    – chepner
    Dec 16 '13 at 17:46










  • ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
    – Charles Duffy
    Apr 28 '17 at 23:13


















up vote
11
down vote













For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh} instead of the correct $(which sh)






share|improve this answer




























    up vote
    6
    down vote













    Try running the script explicitly using bash command rather than just executing it as executable.






    share|improve this answer

















    • 3




      Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
      – fedorqui
      Dec 16 '13 at 16:23


















    up vote
    1
    down vote













    Also, make sure you don't have an empty string for the first line of your script.



    i.e. make sure #!/bin/bash is the very first line of your script.






    share|improve this answer




























      up vote
      0
      down vote













      Both - bash or dash - work, but the syntax needs to be:



      FILENAME=/my/complex/path/name.ext
      NEWNAME=${FILENAME%ext}new





      share|improve this answer





















      • That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
        – Charles Duffy
        Apr 28 '17 at 23:14













      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',
      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%2f20615217%2fbash-bad-substitution%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      136
      down vote



      accepted










      The default shell (/bin/sh) under Ubuntu points to dash, not bash.



      me@pc:~$ readlink -f $(which sh)
      /bin/dash


      So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.



      Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.






      share|improve this answer

















      • 1




        He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
        – DanFromGermany
        Dec 1 '15 at 10:31








      • 2




        @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
        – Vanni Totaro
        Dec 1 '15 at 11:10








      • 1




        I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
        – DanFromGermany
        Dec 1 '15 at 12:47






      • 2




        @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
        – Vanni Totaro
        Dec 1 '15 at 13:27






      • 1




        Change #!/bin/sh to #!/bin/bash work to me.
        – Evi Song
        Oct 23 at 6:18















      up vote
      136
      down vote



      accepted










      The default shell (/bin/sh) under Ubuntu points to dash, not bash.



      me@pc:~$ readlink -f $(which sh)
      /bin/dash


      So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.



      Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.






      share|improve this answer

















      • 1




        He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
        – DanFromGermany
        Dec 1 '15 at 10:31








      • 2




        @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
        – Vanni Totaro
        Dec 1 '15 at 11:10








      • 1




        I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
        – DanFromGermany
        Dec 1 '15 at 12:47






      • 2




        @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
        – Vanni Totaro
        Dec 1 '15 at 13:27






      • 1




        Change #!/bin/sh to #!/bin/bash work to me.
        – Evi Song
        Oct 23 at 6:18













      up vote
      136
      down vote



      accepted







      up vote
      136
      down vote



      accepted






      The default shell (/bin/sh) under Ubuntu points to dash, not bash.



      me@pc:~$ readlink -f $(which sh)
      /bin/dash


      So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.



      Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.






      share|improve this answer












      The default shell (/bin/sh) under Ubuntu points to dash, not bash.



      me@pc:~$ readlink -f $(which sh)
      /bin/dash


      So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.



      Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 16 '13 at 16:44









      Vanni Totaro

      2,92321632




      2,92321632








      • 1




        He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
        – DanFromGermany
        Dec 1 '15 at 10:31








      • 2




        @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
        – Vanni Totaro
        Dec 1 '15 at 11:10








      • 1




        I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
        – DanFromGermany
        Dec 1 '15 at 12:47






      • 2




        @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
        – Vanni Totaro
        Dec 1 '15 at 13:27






      • 1




        Change #!/bin/sh to #!/bin/bash work to me.
        – Evi Song
        Oct 23 at 6:18














      • 1




        He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
        – DanFromGermany
        Dec 1 '15 at 10:31








      • 2




        @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
        – Vanni Totaro
        Dec 1 '15 at 11:10








      • 1




        I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
        – DanFromGermany
        Dec 1 '15 at 12:47






      • 2




        @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
        – Vanni Totaro
        Dec 1 '15 at 13:27






      • 1




        Change #!/bin/sh to #!/bin/bash work to me.
        – Evi Song
        Oct 23 at 6:18








      1




      1




      He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
      – DanFromGermany
      Dec 1 '15 at 10:31






      He is using /bin/bash so your answer does not fit?! Where do you read he's using /bin/sh or sh script.sh ?
      – DanFromGermany
      Dec 1 '15 at 10:31






      2




      2




      @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
      – Vanni Totaro
      Dec 1 '15 at 11:10






      @DanFromGermany because that's the only reason for that error, i.e. he's running the script in a way that doesn't consider the hashbang, and that bash syntax is not supported by some other shell (probably dash). Questions don't always contain all the needed details, and we must join the dots... anyway feel free to downvote my answer.
      – Vanni Totaro
      Dec 1 '15 at 11:10






      1




      1




      I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
      – DanFromGermany
      Dec 1 '15 at 12:47




      I dont need to downvote. I have the same error message bad substitution and I'm just trying to gather information but this question doesn't help because it has too few information.
      – DanFromGermany
      Dec 1 '15 at 12:47




      2




      2




      @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
      – Vanni Totaro
      Dec 1 '15 at 13:27




      @DanFromGermany you could try posting your own question, maybe it's not exactly the same problem.
      – Vanni Totaro
      Dec 1 '15 at 13:27




      1




      1




      Change #!/bin/sh to #!/bin/bash work to me.
      – Evi Song
      Oct 23 at 6:18




      Change #!/bin/sh to #!/bin/bash work to me.
      – Evi Song
      Oct 23 at 6:18












      up vote
      43
      down vote













      I had the same problem. Make sure your script didnt have



      #!/bin/sh 


      at the top of your script. Instead, you should add



      #!/bin/bash





      share|improve this answer



















      • 4




        I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
        – whyisyoung
        Apr 9 '15 at 2:13












      • If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
        – Jamie
        Aug 20 '16 at 9:56






      • 1




        @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
        – user13107
        Sep 7 '16 at 1:13






      • 1




        @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
        – bfontaine
        Oct 4 at 14:21















      up vote
      43
      down vote













      I had the same problem. Make sure your script didnt have



      #!/bin/sh 


      at the top of your script. Instead, you should add



      #!/bin/bash





      share|improve this answer



















      • 4




        I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
        – whyisyoung
        Apr 9 '15 at 2:13












      • If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
        – Jamie
        Aug 20 '16 at 9:56






      • 1




        @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
        – user13107
        Sep 7 '16 at 1:13






      • 1




        @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
        – bfontaine
        Oct 4 at 14:21













      up vote
      43
      down vote










      up vote
      43
      down vote









      I had the same problem. Make sure your script didnt have



      #!/bin/sh 


      at the top of your script. Instead, you should add



      #!/bin/bash





      share|improve this answer














      I had the same problem. Make sure your script didnt have



      #!/bin/sh 


      at the top of your script. Instead, you should add



      #!/bin/bash






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 24 '14 at 19:39









      ZeMoon

      15.3k34281




      15.3k34281










      answered Apr 24 '14 at 19:02









      Guest

      43942




      43942








      • 4




        I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
        – whyisyoung
        Apr 9 '15 at 2:13












      • If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
        – Jamie
        Aug 20 '16 at 9:56






      • 1




        @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
        – user13107
        Sep 7 '16 at 1:13






      • 1




        @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
        – bfontaine
        Oct 4 at 14:21














      • 4




        I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
        – whyisyoung
        Apr 9 '15 at 2:13












      • If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
        – Jamie
        Aug 20 '16 at 9:56






      • 1




        @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
        – user13107
        Sep 7 '16 at 1:13






      • 1




        @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
        – bfontaine
        Oct 4 at 14:21








      4




      4




      I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
      – whyisyoung
      Apr 9 '15 at 2:13






      I used #!bin/bash and sh script.sh, it still gave me the error message. Then ./script.sh works.
      – whyisyoung
      Apr 9 '15 at 2:13














      If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
      – Jamie
      Aug 20 '16 at 9:56




      If your file is missing a shebang at the top adding #!/bin/bash will also fix the Bad substitution.
      – Jamie
      Aug 20 '16 at 9:56




      1




      1




      @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
      – user13107
      Sep 7 '16 at 1:13




      @whyisyoung your variable could be having a dot (.) in its name. It gives bad subst. error.
      – user13107
      Sep 7 '16 at 1:13




      1




      1




      @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
      – bfontaine
      Oct 4 at 14:21




      @whyisyoung the #! line is used only when you execute your script directly. If you use sh script.sh the line is completely ignored.
      – bfontaine
      Oct 4 at 14:21










      up vote
      18
      down vote













      Your script syntax is valid bash and good.



      Possible causes for the failure:




      1. Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
        Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.


      2. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.







      share|improve this answer

















      • 7




        I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
        – chepner
        Dec 16 '13 at 17:46










      • ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
        – Charles Duffy
        Apr 28 '17 at 23:13















      up vote
      18
      down vote













      Your script syntax is valid bash and good.



      Possible causes for the failure:




      1. Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
        Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.


      2. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.







      share|improve this answer

















      • 7




        I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
        – chepner
        Dec 16 '13 at 17:46










      • ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
        – Charles Duffy
        Apr 28 '17 at 23:13













      up vote
      18
      down vote










      up vote
      18
      down vote









      Your script syntax is valid bash and good.



      Possible causes for the failure:




      1. Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
        Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.


      2. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.







      share|improve this answer












      Your script syntax is valid bash and good.



      Possible causes for the failure:




      1. Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
        Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.


      2. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.








      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 16 '13 at 16:35









      P.P.

      74.3k11101152




      74.3k11101152








      • 7




        I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
        – chepner
        Dec 16 '13 at 17:46










      • ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
        – Charles Duffy
        Apr 28 '17 at 23:13














      • 7




        I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
        – chepner
        Dec 16 '13 at 17:46










      • ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
        – Charles Duffy
        Apr 28 '17 at 23:13








      7




      7




      I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
      – chepner
      Dec 16 '13 at 17:46




      I'd be surprised if /bin/bash (not /bin/sh) were ever linked to a different shell.
      – chepner
      Dec 16 '13 at 17:46












      ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
      – Charles Duffy
      Apr 28 '17 at 23:13




      ksh is actually where most of bash's syntax extensions originated from; it certainly has the specific parameter expansion syntax in question. I wouldn't tend to suggest calling it out as a shell unlikely to be capable.
      – Charles Duffy
      Apr 28 '17 at 23:13










      up vote
      11
      down vote













      For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh} instead of the correct $(which sh)






      share|improve this answer

























        up vote
        11
        down vote













        For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh} instead of the correct $(which sh)






        share|improve this answer























          up vote
          11
          down vote










          up vote
          11
          down vote









          For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh} instead of the correct $(which sh)






          share|improve this answer












          For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh} instead of the correct $(which sh)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 17 '16 at 9:33









          Nacho Coloma

          4,54312940




          4,54312940






















              up vote
              6
              down vote













              Try running the script explicitly using bash command rather than just executing it as executable.






              share|improve this answer

















              • 3




                Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
                – fedorqui
                Dec 16 '13 at 16:23















              up vote
              6
              down vote













              Try running the script explicitly using bash command rather than just executing it as executable.






              share|improve this answer

















              • 3




                Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
                – fedorqui
                Dec 16 '13 at 16:23













              up vote
              6
              down vote










              up vote
              6
              down vote









              Try running the script explicitly using bash command rather than just executing it as executable.






              share|improve this answer












              Try running the script explicitly using bash command rather than just executing it as executable.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 16 '13 at 16:18









              Pale Blue Dot

              3,58076095




              3,58076095








              • 3




                Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
                – fedorqui
                Dec 16 '13 at 16:23














              • 3




                Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
                – fedorqui
                Dec 16 '13 at 16:23








              3




              3




              Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
              – fedorqui
              Dec 16 '13 at 16:23




              Good one. It would be helpful to add some sample output to make it more clear, by using sh script and bash script... my suggestion :)
              – fedorqui
              Dec 16 '13 at 16:23










              up vote
              1
              down vote













              Also, make sure you don't have an empty string for the first line of your script.



              i.e. make sure #!/bin/bash is the very first line of your script.






              share|improve this answer

























                up vote
                1
                down vote













                Also, make sure you don't have an empty string for the first line of your script.



                i.e. make sure #!/bin/bash is the very first line of your script.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Also, make sure you don't have an empty string for the first line of your script.



                  i.e. make sure #!/bin/bash is the very first line of your script.






                  share|improve this answer












                  Also, make sure you don't have an empty string for the first line of your script.



                  i.e. make sure #!/bin/bash is the very first line of your script.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 5 '16 at 18:23









                  wizurd

                  2,13031940




                  2,13031940






















                      up vote
                      0
                      down vote













                      Both - bash or dash - work, but the syntax needs to be:



                      FILENAME=/my/complex/path/name.ext
                      NEWNAME=${FILENAME%ext}new





                      share|improve this answer





















                      • That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
                        – Charles Duffy
                        Apr 28 '17 at 23:14

















                      up vote
                      0
                      down vote













                      Both - bash or dash - work, but the syntax needs to be:



                      FILENAME=/my/complex/path/name.ext
                      NEWNAME=${FILENAME%ext}new





                      share|improve this answer





















                      • That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
                        – Charles Duffy
                        Apr 28 '17 at 23:14















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Both - bash or dash - work, but the syntax needs to be:



                      FILENAME=/my/complex/path/name.ext
                      NEWNAME=${FILENAME%ext}new





                      share|improve this answer












                      Both - bash or dash - work, but the syntax needs to be:



                      FILENAME=/my/complex/path/name.ext
                      NEWNAME=${FILENAME%ext}new






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 10 '16 at 10:03









                      Hagen

                      1




                      1












                      • That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
                        – Charles Duffy
                        Apr 28 '17 at 23:14




















                      • That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
                        – Charles Duffy
                        Apr 28 '17 at 23:14


















                      That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
                      – Charles Duffy
                      Apr 28 '17 at 23:14






                      That's a completely different operation. Also, since the OP was following good practices by using lower-case variable names (see pubs.opengroup.org/onlinepubs/9699919799/basedefs/… -- uppercase names are used for variables with meaning to the OS or shell; lowercase names are reserved for application use), it behooves to do likewise.
                      – Charles Duffy
                      Apr 28 '17 at 23:14




















                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f20615217%2fbash-bad-substitution%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







                      這個網誌中的熱門文章

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Xamarin.form Move up view when keyboard appear

                      JBPM : POST request for execute process go wrong