Dynamic variables in shell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












I have a following script that will take input (source path) from user and it will attach the volume inside docker container



echo -n "Enter the source path: "

read path

docker run -v $path:/opt/$path/ fedora


The problem is, i want to make a loop, so that the user can provide multiple source path and it can be attached to docker container.



E.g.



docker run -v $path1:/opt/$path1 -v $path2:/opt/$path2


etc , these number of $path variable depends on the user inputs.










share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    I have a following script that will take input (source path) from user and it will attach the volume inside docker container



    echo -n "Enter the source path: "

    read path

    docker run -v $path:/opt/$path/ fedora


    The problem is, i want to make a loop, so that the user can provide multiple source path and it can be attached to docker container.



    E.g.



    docker run -v $path1:/opt/$path1 -v $path2:/opt/$path2


    etc , these number of $path variable depends on the user inputs.










    share|improve this question

























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have a following script that will take input (source path) from user and it will attach the volume inside docker container



      echo -n "Enter the source path: "

      read path

      docker run -v $path:/opt/$path/ fedora


      The problem is, i want to make a loop, so that the user can provide multiple source path and it can be attached to docker container.



      E.g.



      docker run -v $path1:/opt/$path1 -v $path2:/opt/$path2


      etc , these number of $path variable depends on the user inputs.










      share|improve this question















      I have a following script that will take input (source path) from user and it will attach the volume inside docker container



      echo -n "Enter the source path: "

      read path

      docker run -v $path:/opt/$path/ fedora


      The problem is, i want to make a loop, so that the user can provide multiple source path and it can be attached to docker container.



      E.g.



      docker run -v $path1:/opt/$path1 -v $path2:/opt/$path2


      etc , these number of $path variable depends on the user inputs.







      shell-script shell docker variable-substitution






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 1 at 10:31









      GAD3R

      22.9k164895




      22.9k164895










      asked Aug 31 at 3:59









      smc

      366




      366




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          In bash, you should use an array to hold the paths as read from the user. In general, it's better to keep separate strings (pathnames) separate rather than to concatenate them into a single string that you later have to correctly parse to extract the original constituent strings.



          #!/bin/bash

          echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
          mypaths=()
          while IFS= read -r -p 'Path: ' thepath; do
          mypaths+=( -v "$thepath:/opt/$thepath" )
          done

          docker run "$mypaths[@]" fedora


          Here, the user is prompted to input a path several times until they press Ctrl+D. The paths entered are saved in the mypaths array, which is laid out in such a way that docker may use it directly.



          Once there are no more paths to read, the docker command is called. The "$mypaths[@]" will be expanded to the individually quoted elements of the mypaths array. Since the entries of the array are stored the way they are (with -v as a separate element before each specially formatted pathname:/opt/pathname string), this will be correctly interpreted by the shell and by docker. The only characters that will not be tolerated in pathnames by the above code are newlines, since these are separating the lines read by read.



          The above script would also accept input redirected from a text file containing a single path per line of input.



          Note that the quoting is important. Without the double quotes around the variable expansions, you would not be able to use paths containing whitespace, and you would potentially also have issues with paths containing characters special to the shell.



          Related:



          • Why does my shell script choke on whitespace or other special characters?


          For non-bash (sh) shells:



          #!/bin/sh

          echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
          set --
          while printf 'Path: ' >&2 && IFS= read -r thepath; do
          set -- "$@" -v "$thepath:/opt/$thepath"
          done

          docker run "$@" fedora


          Here we use the list of positional parameters instead of an array (since arrays other than $@ are not available in sh in general), but the workflow is otherwise identical apart from printing the prompt explicitly with printf.




          Implementing the suggestion at the end of Stéphane Chazelas' comment, so that the script takes pathnames on its command line instead of reading them from its standard input. This allows the user to pass arbitrary pathnames to the script, even those that read can't easily read or a user can't easily type on a keyboard.



          For bash using an array:



          #!/bin/bash

          for pathname do
          mypaths+=( -v "$pathname:/opt/$pathname" )
          done

          docker run "$mypaths[@]" fedora


          For sh using the list of positional parameters:



          #!/bin/sh

          for pathname do
          shift
          set -- "$@" -v "$pathname:/opt/$pathname"
          done

          docker run "$@" fedora


          Both of these would be run like



          ./script.sh path1 path2 path3 ...





          share|improve this answer


















          • 1




            Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
            – Stéphane Chazelas
            Sep 1 at 8:42










          • @StéphaneChazelas Updated with your suggestion from that last sentence.
            – Kusalananda
            Sep 1 at 9:34

















          up vote
          3
          down vote













          Try this, the directory names must not contain spaces:



          #!/bin/bash

          echo -n "Enter the Directories, space separated : "
          read dirs

          docker run $( set -- $dirs; for path; do echo -v $path:/opt/$path/; done ) fedora


          Enter something like foo bar, and it will run



          docker run -v foo:/opt/foo/ -v bar:/opt/bar/ fedora





          share|improve this answer



























            up vote
            2
            down vote













            #!/bin/bash

            echo -n "Enter the Path : "
            read path
            echo -n "docker run "
            echo "$path" | tr " " "n" | while read value
            do
            echo -n "-v $value:/opt/$value "
            done





            share|improve this answer






















            • thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
              – smc
              Aug 31 at 4:49











            • try the edited answer
              – Kamaraj
              Aug 31 at 5:38










            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f465914%2fdynamic-variables-in-shell%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            6
            down vote



            accepted










            In bash, you should use an array to hold the paths as read from the user. In general, it's better to keep separate strings (pathnames) separate rather than to concatenate them into a single string that you later have to correctly parse to extract the original constituent strings.



            #!/bin/bash

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            mypaths=()
            while IFS= read -r -p 'Path: ' thepath; do
            mypaths+=( -v "$thepath:/opt/$thepath" )
            done

            docker run "$mypaths[@]" fedora


            Here, the user is prompted to input a path several times until they press Ctrl+D. The paths entered are saved in the mypaths array, which is laid out in such a way that docker may use it directly.



            Once there are no more paths to read, the docker command is called. The "$mypaths[@]" will be expanded to the individually quoted elements of the mypaths array. Since the entries of the array are stored the way they are (with -v as a separate element before each specially formatted pathname:/opt/pathname string), this will be correctly interpreted by the shell and by docker. The only characters that will not be tolerated in pathnames by the above code are newlines, since these are separating the lines read by read.



            The above script would also accept input redirected from a text file containing a single path per line of input.



            Note that the quoting is important. Without the double quotes around the variable expansions, you would not be able to use paths containing whitespace, and you would potentially also have issues with paths containing characters special to the shell.



            Related:



            • Why does my shell script choke on whitespace or other special characters?


            For non-bash (sh) shells:



            #!/bin/sh

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            set --
            while printf 'Path: ' >&2 && IFS= read -r thepath; do
            set -- "$@" -v "$thepath:/opt/$thepath"
            done

            docker run "$@" fedora


            Here we use the list of positional parameters instead of an array (since arrays other than $@ are not available in sh in general), but the workflow is otherwise identical apart from printing the prompt explicitly with printf.




            Implementing the suggestion at the end of Stéphane Chazelas' comment, so that the script takes pathnames on its command line instead of reading them from its standard input. This allows the user to pass arbitrary pathnames to the script, even those that read can't easily read or a user can't easily type on a keyboard.



            For bash using an array:



            #!/bin/bash

            for pathname do
            mypaths+=( -v "$pathname:/opt/$pathname" )
            done

            docker run "$mypaths[@]" fedora


            For sh using the list of positional parameters:



            #!/bin/sh

            for pathname do
            shift
            set -- "$@" -v "$pathname:/opt/$pathname"
            done

            docker run "$@" fedora


            Both of these would be run like



            ./script.sh path1 path2 path3 ...





            share|improve this answer


















            • 1




              Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
              – Stéphane Chazelas
              Sep 1 at 8:42










            • @StéphaneChazelas Updated with your suggestion from that last sentence.
              – Kusalananda
              Sep 1 at 9:34














            up vote
            6
            down vote



            accepted










            In bash, you should use an array to hold the paths as read from the user. In general, it's better to keep separate strings (pathnames) separate rather than to concatenate them into a single string that you later have to correctly parse to extract the original constituent strings.



            #!/bin/bash

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            mypaths=()
            while IFS= read -r -p 'Path: ' thepath; do
            mypaths+=( -v "$thepath:/opt/$thepath" )
            done

            docker run "$mypaths[@]" fedora


            Here, the user is prompted to input a path several times until they press Ctrl+D. The paths entered are saved in the mypaths array, which is laid out in such a way that docker may use it directly.



            Once there are no more paths to read, the docker command is called. The "$mypaths[@]" will be expanded to the individually quoted elements of the mypaths array. Since the entries of the array are stored the way they are (with -v as a separate element before each specially formatted pathname:/opt/pathname string), this will be correctly interpreted by the shell and by docker. The only characters that will not be tolerated in pathnames by the above code are newlines, since these are separating the lines read by read.



            The above script would also accept input redirected from a text file containing a single path per line of input.



            Note that the quoting is important. Without the double quotes around the variable expansions, you would not be able to use paths containing whitespace, and you would potentially also have issues with paths containing characters special to the shell.



            Related:



            • Why does my shell script choke on whitespace or other special characters?


            For non-bash (sh) shells:



            #!/bin/sh

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            set --
            while printf 'Path: ' >&2 && IFS= read -r thepath; do
            set -- "$@" -v "$thepath:/opt/$thepath"
            done

            docker run "$@" fedora


            Here we use the list of positional parameters instead of an array (since arrays other than $@ are not available in sh in general), but the workflow is otherwise identical apart from printing the prompt explicitly with printf.




            Implementing the suggestion at the end of Stéphane Chazelas' comment, so that the script takes pathnames on its command line instead of reading them from its standard input. This allows the user to pass arbitrary pathnames to the script, even those that read can't easily read or a user can't easily type on a keyboard.



            For bash using an array:



            #!/bin/bash

            for pathname do
            mypaths+=( -v "$pathname:/opt/$pathname" )
            done

            docker run "$mypaths[@]" fedora


            For sh using the list of positional parameters:



            #!/bin/sh

            for pathname do
            shift
            set -- "$@" -v "$pathname:/opt/$pathname"
            done

            docker run "$@" fedora


            Both of these would be run like



            ./script.sh path1 path2 path3 ...





            share|improve this answer


















            • 1




              Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
              – Stéphane Chazelas
              Sep 1 at 8:42










            • @StéphaneChazelas Updated with your suggestion from that last sentence.
              – Kusalananda
              Sep 1 at 9:34












            up vote
            6
            down vote



            accepted







            up vote
            6
            down vote



            accepted






            In bash, you should use an array to hold the paths as read from the user. In general, it's better to keep separate strings (pathnames) separate rather than to concatenate them into a single string that you later have to correctly parse to extract the original constituent strings.



            #!/bin/bash

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            mypaths=()
            while IFS= read -r -p 'Path: ' thepath; do
            mypaths+=( -v "$thepath:/opt/$thepath" )
            done

            docker run "$mypaths[@]" fedora


            Here, the user is prompted to input a path several times until they press Ctrl+D. The paths entered are saved in the mypaths array, which is laid out in such a way that docker may use it directly.



            Once there are no more paths to read, the docker command is called. The "$mypaths[@]" will be expanded to the individually quoted elements of the mypaths array. Since the entries of the array are stored the way they are (with -v as a separate element before each specially formatted pathname:/opt/pathname string), this will be correctly interpreted by the shell and by docker. The only characters that will not be tolerated in pathnames by the above code are newlines, since these are separating the lines read by read.



            The above script would also accept input redirected from a text file containing a single path per line of input.



            Note that the quoting is important. Without the double quotes around the variable expansions, you would not be able to use paths containing whitespace, and you would potentially also have issues with paths containing characters special to the shell.



            Related:



            • Why does my shell script choke on whitespace or other special characters?


            For non-bash (sh) shells:



            #!/bin/sh

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            set --
            while printf 'Path: ' >&2 && IFS= read -r thepath; do
            set -- "$@" -v "$thepath:/opt/$thepath"
            done

            docker run "$@" fedora


            Here we use the list of positional parameters instead of an array (since arrays other than $@ are not available in sh in general), but the workflow is otherwise identical apart from printing the prompt explicitly with printf.




            Implementing the suggestion at the end of Stéphane Chazelas' comment, so that the script takes pathnames on its command line instead of reading them from its standard input. This allows the user to pass arbitrary pathnames to the script, even those that read can't easily read or a user can't easily type on a keyboard.



            For bash using an array:



            #!/bin/bash

            for pathname do
            mypaths+=( -v "$pathname:/opt/$pathname" )
            done

            docker run "$mypaths[@]" fedora


            For sh using the list of positional parameters:



            #!/bin/sh

            for pathname do
            shift
            set -- "$@" -v "$pathname:/opt/$pathname"
            done

            docker run "$@" fedora


            Both of these would be run like



            ./script.sh path1 path2 path3 ...





            share|improve this answer














            In bash, you should use an array to hold the paths as read from the user. In general, it's better to keep separate strings (pathnames) separate rather than to concatenate them into a single string that you later have to correctly parse to extract the original constituent strings.



            #!/bin/bash

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            mypaths=()
            while IFS= read -r -p 'Path: ' thepath; do
            mypaths+=( -v "$thepath:/opt/$thepath" )
            done

            docker run "$mypaths[@]" fedora


            Here, the user is prompted to input a path several times until they press Ctrl+D. The paths entered are saved in the mypaths array, which is laid out in such a way that docker may use it directly.



            Once there are no more paths to read, the docker command is called. The "$mypaths[@]" will be expanded to the individually quoted elements of the mypaths array. Since the entries of the array are stored the way they are (with -v as a separate element before each specially formatted pathname:/opt/pathname string), this will be correctly interpreted by the shell and by docker. The only characters that will not be tolerated in pathnames by the above code are newlines, since these are separating the lines read by read.



            The above script would also accept input redirected from a text file containing a single path per line of input.



            Note that the quoting is important. Without the double quotes around the variable expansions, you would not be able to use paths containing whitespace, and you would potentially also have issues with paths containing characters special to the shell.



            Related:



            • Why does my shell script choke on whitespace or other special characters?


            For non-bash (sh) shells:



            #!/bin/sh

            echo 'Enter paths, one by one followed by Enter. End input with Ctrl+D' >&2
            set --
            while printf 'Path: ' >&2 && IFS= read -r thepath; do
            set -- "$@" -v "$thepath:/opt/$thepath"
            done

            docker run "$@" fedora


            Here we use the list of positional parameters instead of an array (since arrays other than $@ are not available in sh in general), but the workflow is otherwise identical apart from printing the prompt explicitly with printf.




            Implementing the suggestion at the end of Stéphane Chazelas' comment, so that the script takes pathnames on its command line instead of reading them from its standard input. This allows the user to pass arbitrary pathnames to the script, even those that read can't easily read or a user can't easily type on a keyboard.



            For bash using an array:



            #!/bin/bash

            for pathname do
            mypaths+=( -v "$pathname:/opt/$pathname" )
            done

            docker run "$mypaths[@]" fedora


            For sh using the list of positional parameters:



            #!/bin/sh

            for pathname do
            shift
            set -- "$@" -v "$pathname:/opt/$pathname"
            done

            docker run "$@" fedora


            Both of these would be run like



            ./script.sh path1 path2 path3 ...






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 1 at 9:47

























            answered Aug 31 at 5:59









            Kusalananda

            107k14209331




            107k14209331







            • 1




              Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
              – Stéphane Chazelas
              Sep 1 at 8:42










            • @StéphaneChazelas Updated with your suggestion from that last sentence.
              – Kusalananda
              Sep 1 at 9:34












            • 1




              Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
              – Stéphane Chazelas
              Sep 1 at 8:42










            • @StéphaneChazelas Updated with your suggestion from that last sentence.
              – Kusalananda
              Sep 1 at 9:34







            1




            1




            Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
            – Stéphane Chazelas
            Sep 1 at 8:42




            Note that prompting the users with path one per line like that means it can't accept arbitrary paths (like those that contain newline characters or byte values that cannot easily be entered on the keyboard. Even if you use bash's read -e where the user can use completion, that will still not allow users to enter paths with newlines (you'd need zsh's vared). Best would be to take the list of files from the arguments of the script, where the user can use the quoting or completion features of their shell to pass the arbitrary file names.
            – Stéphane Chazelas
            Sep 1 at 8:42












            @StéphaneChazelas Updated with your suggestion from that last sentence.
            – Kusalananda
            Sep 1 at 9:34




            @StéphaneChazelas Updated with your suggestion from that last sentence.
            – Kusalananda
            Sep 1 at 9:34












            up vote
            3
            down vote













            Try this, the directory names must not contain spaces:



            #!/bin/bash

            echo -n "Enter the Directories, space separated : "
            read dirs

            docker run $( set -- $dirs; for path; do echo -v $path:/opt/$path/; done ) fedora


            Enter something like foo bar, and it will run



            docker run -v foo:/opt/foo/ -v bar:/opt/bar/ fedora





            share|improve this answer
























              up vote
              3
              down vote













              Try this, the directory names must not contain spaces:



              #!/bin/bash

              echo -n "Enter the Directories, space separated : "
              read dirs

              docker run $( set -- $dirs; for path; do echo -v $path:/opt/$path/; done ) fedora


              Enter something like foo bar, and it will run



              docker run -v foo:/opt/foo/ -v bar:/opt/bar/ fedora





              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                Try this, the directory names must not contain spaces:



                #!/bin/bash

                echo -n "Enter the Directories, space separated : "
                read dirs

                docker run $( set -- $dirs; for path; do echo -v $path:/opt/$path/; done ) fedora


                Enter something like foo bar, and it will run



                docker run -v foo:/opt/foo/ -v bar:/opt/bar/ fedora





                share|improve this answer












                Try this, the directory names must not contain spaces:



                #!/bin/bash

                echo -n "Enter the Directories, space separated : "
                read dirs

                docker run $( set -- $dirs; for path; do echo -v $path:/opt/$path/; done ) fedora


                Enter something like foo bar, and it will run



                docker run -v foo:/opt/foo/ -v bar:/opt/bar/ fedora






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 31 at 5:27









                RalfFriedl

                3,9601625




                3,9601625




















                    up vote
                    2
                    down vote













                    #!/bin/bash

                    echo -n "Enter the Path : "
                    read path
                    echo -n "docker run "
                    echo "$path" | tr " " "n" | while read value
                    do
                    echo -n "-v $value:/opt/$value "
                    done





                    share|improve this answer






















                    • thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
                      – smc
                      Aug 31 at 4:49











                    • try the edited answer
                      – Kamaraj
                      Aug 31 at 5:38














                    up vote
                    2
                    down vote













                    #!/bin/bash

                    echo -n "Enter the Path : "
                    read path
                    echo -n "docker run "
                    echo "$path" | tr " " "n" | while read value
                    do
                    echo -n "-v $value:/opt/$value "
                    done





                    share|improve this answer






















                    • thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
                      – smc
                      Aug 31 at 4:49











                    • try the edited answer
                      – Kamaraj
                      Aug 31 at 5:38












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    #!/bin/bash

                    echo -n "Enter the Path : "
                    read path
                    echo -n "docker run "
                    echo "$path" | tr " " "n" | while read value
                    do
                    echo -n "-v $value:/opt/$value "
                    done





                    share|improve this answer














                    #!/bin/bash

                    echo -n "Enter the Path : "
                    read path
                    echo -n "docker run "
                    echo "$path" | tr " " "n" | while read value
                    do
                    echo -n "-v $value:/opt/$value "
                    done






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 31 at 5:38

























                    answered Aug 31 at 4:22









                    Kamaraj

                    2,9081413




                    2,9081413











                    • thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
                      – smc
                      Aug 31 at 4:49











                    • try the edited answer
                      – Kamaraj
                      Aug 31 at 5:38
















                    • thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
                      – smc
                      Aug 31 at 4:49











                    • try the edited answer
                      – Kamaraj
                      Aug 31 at 5:38















                    thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
                    – smc
                    Aug 31 at 4:49





                    thanks @Kamaraj for the answer. However, I'm getting following output: "Enter the path: /var /tmp docker run -v /var:/opt/var docker run -v /tmp:/opt/tmp". But I'm expecting "Enter the path: /var /tmp docker run -v /var:/opt/var -v /tmp:/opt/tmp "
                    – smc
                    Aug 31 at 4:49













                    try the edited answer
                    – Kamaraj
                    Aug 31 at 5:38




                    try the edited answer
                    – Kamaraj
                    Aug 31 at 5:38

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465914%2fdynamic-variables-in-shell%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)