Get value of the passed argument to script importing variables from another scriptWhy is printf better than echo?Sharing Variables across sub shell scriptsUpdating environment variable in a shell scriptExporting a variable so it appears in a script sourced through sudoHow to add an word for a line where transpose has doneReusing grep outputImporting environment variables settings from another serverImporting environment variables from another script into the current shellI/O error when passing command line argument to a shell scriptColour Errors / Warnings / Information in bash scriptcreate single string argument using heredoc or other technique

Can you please explain this joke: "I'm going bananas is what I tell my bananas before I leave the house"?

Can an old DSLR be upgraded to match modern smartphone image quality

Why does MS SQL allow you to create an illegal column?

Accessing notepad from SSH Server

What is the right way to float a home lab?

Humans meet a distant alien species. How do they standardize? - Units of Measure

How to decline physical affection from a child whose parents are pressuring them?

Did thousands of women die every year due to illegal abortions before Roe v. Wade?

What's the most polite way to tell a manager "shut up and let me work"?

How to detach yourself from a character you're going to kill?

How can I make 20-200 ohm variable resistor look like a 20-240 ohm resistor?

Is it possible to kill all life on Earth?

Could a guilty Boris Johnson be used to cancel Brexit?

Is there any Biblical Basis for 400 years of silence between Old and New Testament?

How can a single Member of the House block a Congressional bill?

Short story written from alien perspective with this line: "It's too bright to look at, so they don't"

Concise way to draw this pyramid

Strange math syntax in old basic listing

Hygienic footwear for prehensile feet?

Opposite of "Squeaky wheel gets the grease"

How can Iron Man's suit withstand this?

Is having a hidden directory under /etc safe?

Why is Colorado so different politically from nearby states?

What people are called boars ("кабан") and why?



Get value of the passed argument to script importing variables from another script


Why is printf better than echo?Sharing Variables across sub shell scriptsUpdating environment variable in a shell scriptExporting a variable so it appears in a script sourced through sudoHow to add an word for a line where transpose has doneReusing grep outputImporting environment variables settings from another serverImporting environment variables from another script into the current shellI/O error when passing command line argument to a shell scriptColour Errors / Warnings / Information in bash scriptcreate single string argument using heredoc or other technique






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.



Here is the script fruitcolour.sh having variables only :



apple="Red"
mango="Yellow"
orange="Orange"
pear="Green"


Here is the main script GetFruitColour.sh :



#!/bin/bash

source fruitcolour.sh

echo "The colour of " $@ " is " $@ "."


For passing apple as argument, I want to get the value of variable apple i.e. Red .



So, When I run ./GetFruitColour.sh apple



It must give output :: The colour of apple is Red.










share|improve this question




























    2















    I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.



    Here is the script fruitcolour.sh having variables only :



    apple="Red"
    mango="Yellow"
    orange="Orange"
    pear="Green"


    Here is the main script GetFruitColour.sh :



    #!/bin/bash

    source fruitcolour.sh

    echo "The colour of " $@ " is " $@ "."


    For passing apple as argument, I want to get the value of variable apple i.e. Red .



    So, When I run ./GetFruitColour.sh apple



    It must give output :: The colour of apple is Red.










    share|improve this question
























      2












      2








      2








      I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.



      Here is the script fruitcolour.sh having variables only :



      apple="Red"
      mango="Yellow"
      orange="Orange"
      pear="Green"


      Here is the main script GetFruitColour.sh :



      #!/bin/bash

      source fruitcolour.sh

      echo "The colour of " $@ " is " $@ "."


      For passing apple as argument, I want to get the value of variable apple i.e. Red .



      So, When I run ./GetFruitColour.sh apple



      It must give output :: The colour of apple is Red.










      share|improve this question














      I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.



      Here is the script fruitcolour.sh having variables only :



      apple="Red"
      mango="Yellow"
      orange="Orange"
      pear="Green"


      Here is the main script GetFruitColour.sh :



      #!/bin/bash

      source fruitcolour.sh

      echo "The colour of " $@ " is " $@ "."


      For passing apple as argument, I want to get the value of variable apple i.e. Red .



      So, When I run ./GetFruitColour.sh apple



      It must give output :: The colour of apple is Red.







      bash shell-script variable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 9 hours ago









      jonny789jonny789

      139110




      139110




















          2 Answers
          2






          active

          oldest

          votes


















          5














          One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.



          To demonstrate:



          apple="Red"
          var="apple"
          echo "$!var"


          Results in:



          Red


          Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.



          As a result, your GetFruitColour.sh script could look like:



          #!/bin/bash

          source ./fruitcolour.sh

          for arg in "$@"
          do
          printf 'The colour of %s is %s.n' "$arg" "$!arg"
          done


          I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).



          I've also changed echo to printf.



          The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:



          $ ./GetFruitColour.sh apple mango
          The colour of apple is Red.
          The colour of mango is Yellow.


          Do note that there's no error-checking here:



          $ ./GetFruitColour.sh foo
          The colour of foo is .


          You may find it easier to use an associative array:



          declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'

          for arg in "$@"
          do
          if [ "$fruits["$arg"]-unset" = "unset" ]
          then
          echo "I do not know the color of $arg"
          else
          printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
          fi
          done





          share|improve this answer























          • That's the most concise explanation of !var that I've seen.

            – Tim Kennedy
            9 hours ago











          • It worked great. Thank you for explaining.

            – jonny789
            9 hours ago


















          2














          You need to use an indirect variable reference:




          If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




          fruitcolor.sh:



          #!/bin/bash

          source fruitcolor.sh

          echo "The color of $1 is $!1"



          $ ./getfruitcolor.sh apple
          The color of apple is Red





          share|improve this answer























            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%2f521996%2fget-value-of-the-passed-argument-to-script-importing-variables-from-another-scri%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









            5














            One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.



            To demonstrate:



            apple="Red"
            var="apple"
            echo "$!var"


            Results in:



            Red


            Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.



            As a result, your GetFruitColour.sh script could look like:



            #!/bin/bash

            source ./fruitcolour.sh

            for arg in "$@"
            do
            printf 'The colour of %s is %s.n' "$arg" "$!arg"
            done


            I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).



            I've also changed echo to printf.



            The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:



            $ ./GetFruitColour.sh apple mango
            The colour of apple is Red.
            The colour of mango is Yellow.


            Do note that there's no error-checking here:



            $ ./GetFruitColour.sh foo
            The colour of foo is .


            You may find it easier to use an associative array:



            declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'

            for arg in "$@"
            do
            if [ "$fruits["$arg"]-unset" = "unset" ]
            then
            echo "I do not know the color of $arg"
            else
            printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
            fi
            done





            share|improve this answer























            • That's the most concise explanation of !var that I've seen.

              – Tim Kennedy
              9 hours ago











            • It worked great. Thank you for explaining.

              – jonny789
              9 hours ago















            5














            One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.



            To demonstrate:



            apple="Red"
            var="apple"
            echo "$!var"


            Results in:



            Red


            Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.



            As a result, your GetFruitColour.sh script could look like:



            #!/bin/bash

            source ./fruitcolour.sh

            for arg in "$@"
            do
            printf 'The colour of %s is %s.n' "$arg" "$!arg"
            done


            I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).



            I've also changed echo to printf.



            The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:



            $ ./GetFruitColour.sh apple mango
            The colour of apple is Red.
            The colour of mango is Yellow.


            Do note that there's no error-checking here:



            $ ./GetFruitColour.sh foo
            The colour of foo is .


            You may find it easier to use an associative array:



            declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'

            for arg in "$@"
            do
            if [ "$fruits["$arg"]-unset" = "unset" ]
            then
            echo "I do not know the color of $arg"
            else
            printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
            fi
            done





            share|improve this answer























            • That's the most concise explanation of !var that I've seen.

              – Tim Kennedy
              9 hours ago











            • It worked great. Thank you for explaining.

              – jonny789
              9 hours ago













            5












            5








            5







            One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.



            To demonstrate:



            apple="Red"
            var="apple"
            echo "$!var"


            Results in:



            Red


            Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.



            As a result, your GetFruitColour.sh script could look like:



            #!/bin/bash

            source ./fruitcolour.sh

            for arg in "$@"
            do
            printf 'The colour of %s is %s.n' "$arg" "$!arg"
            done


            I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).



            I've also changed echo to printf.



            The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:



            $ ./GetFruitColour.sh apple mango
            The colour of apple is Red.
            The colour of mango is Yellow.


            Do note that there's no error-checking here:



            $ ./GetFruitColour.sh foo
            The colour of foo is .


            You may find it easier to use an associative array:



            declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'

            for arg in "$@"
            do
            if [ "$fruits["$arg"]-unset" = "unset" ]
            then
            echo "I do not know the color of $arg"
            else
            printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
            fi
            done





            share|improve this answer













            One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.



            To demonstrate:



            apple="Red"
            var="apple"
            echo "$!var"


            Results in:



            Red


            Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.



            As a result, your GetFruitColour.sh script could look like:



            #!/bin/bash

            source ./fruitcolour.sh

            for arg in "$@"
            do
            printf 'The colour of %s is %s.n' "$arg" "$!arg"
            done


            I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).



            I've also changed echo to printf.



            The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:



            $ ./GetFruitColour.sh apple mango
            The colour of apple is Red.
            The colour of mango is Yellow.


            Do note that there's no error-checking here:



            $ ./GetFruitColour.sh foo
            The colour of foo is .


            You may find it easier to use an associative array:



            declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'

            for arg in "$@"
            do
            if [ "$fruits["$arg"]-unset" = "unset" ]
            then
            echo "I do not know the color of $arg"
            else
            printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
            fi
            done






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 9 hours ago









            Jeff SchallerJeff Schaller

            46k1165150




            46k1165150












            • That's the most concise explanation of !var that I've seen.

              – Tim Kennedy
              9 hours ago











            • It worked great. Thank you for explaining.

              – jonny789
              9 hours ago

















            • That's the most concise explanation of !var that I've seen.

              – Tim Kennedy
              9 hours ago











            • It worked great. Thank you for explaining.

              – jonny789
              9 hours ago
















            That's the most concise explanation of !var that I've seen.

            – Tim Kennedy
            9 hours ago





            That's the most concise explanation of !var that I've seen.

            – Tim Kennedy
            9 hours ago













            It worked great. Thank you for explaining.

            – jonny789
            9 hours ago





            It worked great. Thank you for explaining.

            – jonny789
            9 hours ago













            2














            You need to use an indirect variable reference:




            If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




            fruitcolor.sh:



            #!/bin/bash

            source fruitcolor.sh

            echo "The color of $1 is $!1"



            $ ./getfruitcolor.sh apple
            The color of apple is Red





            share|improve this answer



























              2














              You need to use an indirect variable reference:




              If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




              fruitcolor.sh:



              #!/bin/bash

              source fruitcolor.sh

              echo "The color of $1 is $!1"



              $ ./getfruitcolor.sh apple
              The color of apple is Red





              share|improve this answer

























                2












                2








                2







                You need to use an indirect variable reference:




                If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




                fruitcolor.sh:



                #!/bin/bash

                source fruitcolor.sh

                echo "The color of $1 is $!1"



                $ ./getfruitcolor.sh apple
                The color of apple is Red





                share|improve this answer













                You need to use an indirect variable reference:




                If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




                fruitcolor.sh:



                #!/bin/bash

                source fruitcolor.sh

                echo "The color of $1 is $!1"



                $ ./getfruitcolor.sh apple
                The color of apple is Red






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 9 hours ago









                Jesse_bJesse_b

                15.7k33877




                15.7k33877



























                    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%2f521996%2fget-value-of-the-passed-argument-to-script-importing-variables-from-another-scri%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

                    ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                    Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                    19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу