Set variable conditionally on command line [duplicate]












3















This question already has an answer here:




  • How are parentheses interpreted at the command line?

    2 answers




I want to set a variable if my condition is true on my Ubuntu system.



This proves that my if-statement is correct:



$ (if [ 1 == 1 ]; then echo "hi there"; fi);
hi there


This proves that I can set variables:



$ a=1
$ echo $a
1


This shows that setting a variable in the if-statement DOES NOT work:



$ (if [ 1 == 1 ]; then a=2; fi);
$ echo $a
1


Any ideas why? All my google research indicates that it should work like this...










share|improve this question















marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    3















    This question already has an answer here:




    • How are parentheses interpreted at the command line?

      2 answers




    I want to set a variable if my condition is true on my Ubuntu system.



    This proves that my if-statement is correct:



    $ (if [ 1 == 1 ]; then echo "hi there"; fi);
    hi there


    This proves that I can set variables:



    $ a=1
    $ echo $a
    1


    This shows that setting a variable in the if-statement DOES NOT work:



    $ (if [ 1 == 1 ]; then a=2; fi);
    $ echo $a
    1


    Any ideas why? All my google research indicates that it should work like this...










    share|improve this question















    marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      3












      3








      3








      This question already has an answer here:




      • How are parentheses interpreted at the command line?

        2 answers




      I want to set a variable if my condition is true on my Ubuntu system.



      This proves that my if-statement is correct:



      $ (if [ 1 == 1 ]; then echo "hi there"; fi);
      hi there


      This proves that I can set variables:



      $ a=1
      $ echo $a
      1


      This shows that setting a variable in the if-statement DOES NOT work:



      $ (if [ 1 == 1 ]; then a=2; fi);
      $ echo $a
      1


      Any ideas why? All my google research indicates that it should work like this...










      share|improve this question
















      This question already has an answer here:




      • How are parentheses interpreted at the command line?

        2 answers




      I want to set a variable if my condition is true on my Ubuntu system.



      This proves that my if-statement is correct:



      $ (if [ 1 == 1 ]; then echo "hi there"; fi);
      hi there


      This proves that I can set variables:



      $ a=1
      $ echo $a
      1


      This shows that setting a variable in the if-statement DOES NOT work:



      $ (if [ 1 == 1 ]; then a=2; fi);
      $ echo $a
      1


      Any ideas why? All my google research indicates that it should work like this...





      This question already has an answer here:




      • How are parentheses interpreted at the command line?

        2 answers








      shell command-line






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 16:19









      Barmar

      6,9431223




      6,9431223










      asked Nov 20 at 13:33









      Ron

      1214




      1214




      marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          3 Answers
          3






          active

          oldest

          votes


















          10














          The (...) part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.



          Therefore, remove the subshell:



          if [ 1 = 1 ]; then a=2; fi
          echo "$a"





          share|improve this answer































            5














            This proves that setting a variable in a sub-shell has no lasting effect



            (if [ 1 == 1 ]; then a=2; fi);
            echo $a


            produces



            1


            same as



            (a=2)
            echo $a


            produces



            1


            Solution remove the parenthesis.



            if [ 1 == 1 ]; then a=2; fi;
            echo $a


            produces



            2


            or if you need a sub-shell



            (
            if [ 1 == 1 ]; then a=2; fi;
            echo $a
            )


            produces



            2





            share|improve this answer































              -2














              It done by below method and it worked fine



              _example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
              ================================
              output

              praveen
              2





              share|improve this answer



















              • 4




                Can you explain what the sed command is doing and why you are using it?
                – Kusalananda
                Nov 20 at 18:49






              • 1




                What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                – Jeff Schaller
                Nov 20 at 22:37


















              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              10














              The (...) part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.



              Therefore, remove the subshell:



              if [ 1 = 1 ]; then a=2; fi
              echo "$a"





              share|improve this answer




























                10














                The (...) part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.



                Therefore, remove the subshell:



                if [ 1 = 1 ]; then a=2; fi
                echo "$a"





                share|improve this answer


























                  10












                  10








                  10






                  The (...) part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.



                  Therefore, remove the subshell:



                  if [ 1 = 1 ]; then a=2; fi
                  echo "$a"





                  share|improve this answer














                  The (...) part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.



                  Therefore, remove the subshell:



                  if [ 1 = 1 ]; then a=2; fi
                  echo "$a"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 18:50

























                  answered Nov 20 at 13:44









                  Kusalananda

                  121k16229372




                  121k16229372

























                      5














                      This proves that setting a variable in a sub-shell has no lasting effect



                      (if [ 1 == 1 ]; then a=2; fi);
                      echo $a


                      produces



                      1


                      same as



                      (a=2)
                      echo $a


                      produces



                      1


                      Solution remove the parenthesis.



                      if [ 1 == 1 ]; then a=2; fi;
                      echo $a


                      produces



                      2


                      or if you need a sub-shell



                      (
                      if [ 1 == 1 ]; then a=2; fi;
                      echo $a
                      )


                      produces



                      2





                      share|improve this answer




























                        5














                        This proves that setting a variable in a sub-shell has no lasting effect



                        (if [ 1 == 1 ]; then a=2; fi);
                        echo $a


                        produces



                        1


                        same as



                        (a=2)
                        echo $a


                        produces



                        1


                        Solution remove the parenthesis.



                        if [ 1 == 1 ]; then a=2; fi;
                        echo $a


                        produces



                        2


                        or if you need a sub-shell



                        (
                        if [ 1 == 1 ]; then a=2; fi;
                        echo $a
                        )


                        produces



                        2





                        share|improve this answer


























                          5












                          5








                          5






                          This proves that setting a variable in a sub-shell has no lasting effect



                          (if [ 1 == 1 ]; then a=2; fi);
                          echo $a


                          produces



                          1


                          same as



                          (a=2)
                          echo $a


                          produces



                          1


                          Solution remove the parenthesis.



                          if [ 1 == 1 ]; then a=2; fi;
                          echo $a


                          produces



                          2


                          or if you need a sub-shell



                          (
                          if [ 1 == 1 ]; then a=2; fi;
                          echo $a
                          )


                          produces



                          2





                          share|improve this answer














                          This proves that setting a variable in a sub-shell has no lasting effect



                          (if [ 1 == 1 ]; then a=2; fi);
                          echo $a


                          produces



                          1


                          same as



                          (a=2)
                          echo $a


                          produces



                          1


                          Solution remove the parenthesis.



                          if [ 1 == 1 ]; then a=2; fi;
                          echo $a


                          produces



                          2


                          or if you need a sub-shell



                          (
                          if [ 1 == 1 ]; then a=2; fi;
                          echo $a
                          )


                          produces



                          2






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 20 at 14:12









                          Zanna

                          2,5461023




                          2,5461023










                          answered Nov 20 at 13:42









                          ctrl-alt-delor

                          10.8k41957




                          10.8k41957























                              -2














                              It done by below method and it worked fine



                              _example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
                              ================================
                              output

                              praveen
                              2





                              share|improve this answer



















                              • 4




                                Can you explain what the sed command is doing and why you are using it?
                                – Kusalananda
                                Nov 20 at 18:49






                              • 1




                                What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                                – Jeff Schaller
                                Nov 20 at 22:37
















                              -2














                              It done by below method and it worked fine



                              _example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
                              ================================
                              output

                              praveen
                              2





                              share|improve this answer



















                              • 4




                                Can you explain what the sed command is doing and why you are using it?
                                – Kusalananda
                                Nov 20 at 18:49






                              • 1




                                What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                                – Jeff Schaller
                                Nov 20 at 22:37














                              -2












                              -2








                              -2






                              It done by below method and it worked fine



                              _example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
                              ================================
                              output

                              praveen
                              2





                              share|improve this answer














                              It done by below method and it worked fine



                              _example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
                              ================================
                              output

                              praveen
                              2






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 20 at 22:54









                              ilkkachu

                              55.7k783152




                              55.7k783152










                              answered Nov 20 at 18:19









                              Praveen Kumar BS

                              1,206138




                              1,206138








                              • 4




                                Can you explain what the sed command is doing and why you are using it?
                                – Kusalananda
                                Nov 20 at 18:49






                              • 1




                                What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                                – Jeff Schaller
                                Nov 20 at 22:37














                              • 4




                                Can you explain what the sed command is doing and why you are using it?
                                – Kusalananda
                                Nov 20 at 18:49






                              • 1




                                What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                                – Jeff Schaller
                                Nov 20 at 22:37








                              4




                              4




                              Can you explain what the sed command is doing and why you are using it?
                              – Kusalananda
                              Nov 20 at 18:49




                              Can you explain what the sed command is doing and why you are using it?
                              – Kusalananda
                              Nov 20 at 18:49




                              1




                              1




                              What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                              – Jeff Schaller
                              Nov 20 at 22:37




                              What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
                              – Jeff Schaller
                              Nov 20 at 22:37



                              Popular posts from this blog

                              How to change which sound is reproduced for terminal bell?

                              Can I use Tabulator js library in my java Spring + Thymeleaf project?

                              Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents