Bash variable substitution of variable followed by underscore












10















The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below.



# echo $BUILDNUMBER_

# echo $BUILDNUMBER
230









share|improve this question





























    10















    The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below.



    # echo $BUILDNUMBER_

    # echo $BUILDNUMBER
    230









    share|improve this question



























      10












      10








      10


      0






      The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below.



      # echo $BUILDNUMBER_

      # echo $BUILDNUMBER
      230









      share|improve this question
















      The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below.



      # echo $BUILDNUMBER_

      # echo $BUILDNUMBER
      230






      bash shell-script variable-substitution






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 11 '17 at 10:11









      Kusalananda

      127k16239394




      127k16239394










      asked Apr 11 '17 at 9:45









      Talespin_KitTalespin_Kit

      18019




      18019






















          2 Answers
          2






          active

          oldest

          votes


















          23














          The command echo $BUILDNUMBER_ is going to print the value of variable $BUILDNUMBER_ which is not set (underscore is a valid character for a variable name as explicitly noted by Jeff Schaller)



          You just need to apply brackets around the variable name or use the most rigid printf tool:



          echo "${BUILDNUMBER}_"
          printf '%s_n' "$BUILDNUMBER"


          PS: Always quote your variables.






          share|improve this answer


























          • The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

            – ilkkachu
            Apr 11 '17 at 10:04



















          7














          As George Vassiliou already explained, that's because you're printing the variable $BUILDNUMBER_ instead of $BUILDNUMBER. The best way to get what you want is to use ${BUILDNUMBER}_ as George explained. Here are some more options:



          $ echo "$BUILDNUMBER"_
          230_
          $ echo $BUILDNUMBER"_"
          230_
          $ printf '%s_n' "$BUILDNUMBER"
          230_





          share|improve this answer


























          • @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

            – terdon
            Apr 11 '17 at 21:47













          • Use "$var""_"

            – Kevin
            Apr 11 '17 at 22:03











          • @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

            – terdon
            Apr 11 '17 at 22:53













          • Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

            – Kevin
            Apr 11 '17 at 23:14






          • 6





            I did use that. Right there in my first example

            – terdon
            Apr 11 '17 at 23:45











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f358272%2fbash-variable-substitution-of-variable-followed-by-underscore%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          23














          The command echo $BUILDNUMBER_ is going to print the value of variable $BUILDNUMBER_ which is not set (underscore is a valid character for a variable name as explicitly noted by Jeff Schaller)



          You just need to apply brackets around the variable name or use the most rigid printf tool:



          echo "${BUILDNUMBER}_"
          printf '%s_n' "$BUILDNUMBER"


          PS: Always quote your variables.






          share|improve this answer


























          • The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

            – ilkkachu
            Apr 11 '17 at 10:04
















          23














          The command echo $BUILDNUMBER_ is going to print the value of variable $BUILDNUMBER_ which is not set (underscore is a valid character for a variable name as explicitly noted by Jeff Schaller)



          You just need to apply brackets around the variable name or use the most rigid printf tool:



          echo "${BUILDNUMBER}_"
          printf '%s_n' "$BUILDNUMBER"


          PS: Always quote your variables.






          share|improve this answer


























          • The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

            – ilkkachu
            Apr 11 '17 at 10:04














          23












          23








          23







          The command echo $BUILDNUMBER_ is going to print the value of variable $BUILDNUMBER_ which is not set (underscore is a valid character for a variable name as explicitly noted by Jeff Schaller)



          You just need to apply brackets around the variable name or use the most rigid printf tool:



          echo "${BUILDNUMBER}_"
          printf '%s_n' "$BUILDNUMBER"


          PS: Always quote your variables.






          share|improve this answer















          The command echo $BUILDNUMBER_ is going to print the value of variable $BUILDNUMBER_ which is not set (underscore is a valid character for a variable name as explicitly noted by Jeff Schaller)



          You just need to apply brackets around the variable name or use the most rigid printf tool:



          echo "${BUILDNUMBER}_"
          printf '%s_n' "$BUILDNUMBER"


          PS: Always quote your variables.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 11 '17 at 10:02









          ilkkachu

          57.3k786159




          57.3k786159










          answered Apr 11 '17 at 9:49









          George VasiliouGeorge Vasiliou

          5,66031028




          5,66031028













          • The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

            – ilkkachu
            Apr 11 '17 at 10:04



















          • The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

            – ilkkachu
            Apr 11 '17 at 10:04

















          The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

          – ilkkachu
          Apr 11 '17 at 10:04





          The documentation and the standard use the term "null" for a variable set to an empty string (as opposed to an unset variable). I took the liberty of editing.

          – ilkkachu
          Apr 11 '17 at 10:04













          7














          As George Vassiliou already explained, that's because you're printing the variable $BUILDNUMBER_ instead of $BUILDNUMBER. The best way to get what you want is to use ${BUILDNUMBER}_ as George explained. Here are some more options:



          $ echo "$BUILDNUMBER"_
          230_
          $ echo $BUILDNUMBER"_"
          230_
          $ printf '%s_n' "$BUILDNUMBER"
          230_





          share|improve this answer


























          • @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

            – terdon
            Apr 11 '17 at 21:47













          • Use "$var""_"

            – Kevin
            Apr 11 '17 at 22:03











          • @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

            – terdon
            Apr 11 '17 at 22:53













          • Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

            – Kevin
            Apr 11 '17 at 23:14






          • 6





            I did use that. Right there in my first example

            – terdon
            Apr 11 '17 at 23:45
















          7














          As George Vassiliou already explained, that's because you're printing the variable $BUILDNUMBER_ instead of $BUILDNUMBER. The best way to get what you want is to use ${BUILDNUMBER}_ as George explained. Here are some more options:



          $ echo "$BUILDNUMBER"_
          230_
          $ echo $BUILDNUMBER"_"
          230_
          $ printf '%s_n' "$BUILDNUMBER"
          230_





          share|improve this answer


























          • @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

            – terdon
            Apr 11 '17 at 21:47













          • Use "$var""_"

            – Kevin
            Apr 11 '17 at 22:03











          • @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

            – terdon
            Apr 11 '17 at 22:53













          • Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

            – Kevin
            Apr 11 '17 at 23:14






          • 6





            I did use that. Right there in my first example

            – terdon
            Apr 11 '17 at 23:45














          7












          7








          7







          As George Vassiliou already explained, that's because you're printing the variable $BUILDNUMBER_ instead of $BUILDNUMBER. The best way to get what you want is to use ${BUILDNUMBER}_ as George explained. Here are some more options:



          $ echo "$BUILDNUMBER"_
          230_
          $ echo $BUILDNUMBER"_"
          230_
          $ printf '%s_n' "$BUILDNUMBER"
          230_





          share|improve this answer















          As George Vassiliou already explained, that's because you're printing the variable $BUILDNUMBER_ instead of $BUILDNUMBER. The best way to get what you want is to use ${BUILDNUMBER}_ as George explained. Here are some more options:



          $ echo "$BUILDNUMBER"_
          230_
          $ echo $BUILDNUMBER"_"
          230_
          $ printf '%s_n' "$BUILDNUMBER"
          230_






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 13 '17 at 12:37









          Community

          1




          1










          answered Apr 11 '17 at 10:29









          terdonterdon

          130k32254432




          130k32254432













          • @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

            – terdon
            Apr 11 '17 at 21:47













          • Use "$var""_"

            – Kevin
            Apr 11 '17 at 22:03











          • @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

            – terdon
            Apr 11 '17 at 22:53













          • Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

            – Kevin
            Apr 11 '17 at 23:14






          • 6





            I did use that. Right there in my first example

            – terdon
            Apr 11 '17 at 23:45



















          • @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

            – terdon
            Apr 11 '17 at 21:47













          • Use "$var""_"

            – Kevin
            Apr 11 '17 at 22:03











          • @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

            – terdon
            Apr 11 '17 at 22:53













          • Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

            – Kevin
            Apr 11 '17 at 23:14






          • 6





            I did use that. Right there in my first example

            – terdon
            Apr 11 '17 at 23:45

















          @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

          – terdon
          Apr 11 '17 at 21:47







          @Kevin uhm. Yes, thank you, I know. Which is why I'm quoting my variables even in this silly example where there's no reason to given that we know what the variable holds. Nevertheless, I am quoting for the printf call. I also, however, wanted to demonstrate that you can do both "$var"_ and $var"_" to get the same result. Quoting in the second case, the only instance of an unquoted variable here, would defeat the purpose since "$var_" doesn't expand correctly.

          – terdon
          Apr 11 '17 at 21:47















          Use "$var""_"

          – Kevin
          Apr 11 '17 at 22:03





          Use "$var""_"

          – Kevin
          Apr 11 '17 at 22:03













          @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

          – terdon
          Apr 11 '17 at 22:53







          @Kevin why? You are quoting a string, not a variable. That adds no security, avoids no possible issues, makes absolutely no difference with the split+glob and only adds two unnecessary characters to your code. I could understand claiming that $var"_" is dangerous (it is) but an unquoted underscore has no special meaning in any shell I've heard of and certainly not in bash. If you must quote the simple string, at least use "${var}_".

          – terdon
          Apr 11 '17 at 22:53















          Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

          – Kevin
          Apr 11 '17 at 23:14





          Well then use "$var"_. You're the one who wanted to quote the underscore in the first place.

          – Kevin
          Apr 11 '17 at 23:14




          6




          6





          I did use that. Right there in my first example

          – terdon
          Apr 11 '17 at 23:45





          I did use that. Right there in my first example

          – terdon
          Apr 11 '17 at 23:45


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f358272%2fbash-variable-substitution-of-variable-followed-by-underscore%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

          Histoire des bourses de valeurs

          Why is there Russian traffic in my log files?

          Rename multiple files to decrement number in file name?