To be able to use a variable with $ in its value as current user as well as some other user through the su command

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











up vote
2
down vote

favorite












Suppose a variable value has $ symbol (storing path of a java inner class).



I want to process it as current user as well as some other user (assume current user to be root so that I need not enter password when using the su command).



Example:



path_value=/home/username/filename$1.class

echo $path_value

su username -c "echo $path_value"


The result of first echo:



/home/username/filename$1.class


The results of the second echo inside of su command:



/home/username/filename.class


I want to use the variable such that I would be able to process it at both places with the same value.







share|improve this question

























    up vote
    2
    down vote

    favorite












    Suppose a variable value has $ symbol (storing path of a java inner class).



    I want to process it as current user as well as some other user (assume current user to be root so that I need not enter password when using the su command).



    Example:



    path_value=/home/username/filename$1.class

    echo $path_value

    su username -c "echo $path_value"


    The result of first echo:



    /home/username/filename$1.class


    The results of the second echo inside of su command:



    /home/username/filename.class


    I want to use the variable such that I would be able to process it at both places with the same value.







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Suppose a variable value has $ symbol (storing path of a java inner class).



      I want to process it as current user as well as some other user (assume current user to be root so that I need not enter password when using the su command).



      Example:



      path_value=/home/username/filename$1.class

      echo $path_value

      su username -c "echo $path_value"


      The result of first echo:



      /home/username/filename$1.class


      The results of the second echo inside of su command:



      /home/username/filename.class


      I want to use the variable such that I would be able to process it at both places with the same value.







      share|improve this question













      Suppose a variable value has $ symbol (storing path of a java inner class).



      I want to process it as current user as well as some other user (assume current user to be root so that I need not enter password when using the su command).



      Example:



      path_value=/home/username/filename$1.class

      echo $path_value

      su username -c "echo $path_value"


      The result of first echo:



      /home/username/filename$1.class


      The results of the second echo inside of su command:



      /home/username/filename.class


      I want to use the variable such that I would be able to process it at both places with the same value.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 27 at 4:54









      Vlastimil

      6,2511146115




      6,2511146115









      asked Jun 27 at 4:32









      sureshcskkumar

      111




      111




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          3
          down vote













          Export the value through the environment, then have the inner shell expand it. Assuming username's login shell is Bourne-like¹:



          $ export path_value=/home/username/filename$1.class
          $ su username -c 'echo "$path_value"'
          /home/username/filename$1.class


          This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su should do that, but sudo might.



          If we know that the variable can't contain single quotes², then just single-quoting the expanded string in the inner shell would do:



          $ path_value=/home/username/filename$1.class
          $ su username -c "echo '$path_value'"
          /home/username/filename$1.class


          (Note that the quotes are in the opposite order.)





          ¹ if username's login shell is csh or tcsh, replace echo "$path_value" with echo $path_value:q; if it's rc or derivative, with echo $path_value.




          ² and newline characters if the user's login shell is csh or tcsh, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash. Also beware that arguments starting with - or containing backslashes would be a problem with echo, it's better to use printf '%sn' for arbitrary data






          share|improve this answer



















          • 2




            With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
            – Stéphane Chazelas
            Jun 27 at 7:47

















          up vote
          1
          down vote













          Why not use sudo instead of su?



          # path_value=/home/username/filename$1.class
          # echo "$path_value"
          /home/username/filename$1.class
          # sudo -u username echo "$path_value"
          /home/username/filename$1.class





          share|improve this answer






























            up vote
            0
            down vote













            in the second echo put your variable in single quotes - like:



            su username -c "echo '$path_value'"


            That way echo interprets the content of the variable as a string and the "$"-sign is escaped.






            share|improve this answer




























              up vote
              0
              down vote













              Remember to properly quote all expansions. The shell will not peek inside single quoted strings.



              path_value='/home/username/filename$1.class'
              printf '%sn' "$path_value"


              Outputs /home/username/filename$1.class.



              su username -c "printf '%sn' '$path_value'"


              Identical output.



              In the second command, the current shell will expand the value of path_value inside tho command, as it is double quoted. The inner printf will then print the single quoted string.



              Related:



              • Security implications of forgetting to quote a variable in bash/POSIX shells

              • When is double-quoting necessary?

              • Why is printf better than echo?


              Note that $variable is exactly the same as $variable. Adding ... does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex".






              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',
                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%2f452134%2fto-be-able-to-use-a-variable-with-in-its-value-as-current-user-as-well-as-some%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                3
                down vote













                Export the value through the environment, then have the inner shell expand it. Assuming username's login shell is Bourne-like¹:



                $ export path_value=/home/username/filename$1.class
                $ su username -c 'echo "$path_value"'
                /home/username/filename$1.class


                This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su should do that, but sudo might.



                If we know that the variable can't contain single quotes², then just single-quoting the expanded string in the inner shell would do:



                $ path_value=/home/username/filename$1.class
                $ su username -c "echo '$path_value'"
                /home/username/filename$1.class


                (Note that the quotes are in the opposite order.)





                ¹ if username's login shell is csh or tcsh, replace echo "$path_value" with echo $path_value:q; if it's rc or derivative, with echo $path_value.




                ² and newline characters if the user's login shell is csh or tcsh, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash. Also beware that arguments starting with - or containing backslashes would be a problem with echo, it's better to use printf '%sn' for arbitrary data






                share|improve this answer



















                • 2




                  With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
                  – Stéphane Chazelas
                  Jun 27 at 7:47














                up vote
                3
                down vote













                Export the value through the environment, then have the inner shell expand it. Assuming username's login shell is Bourne-like¹:



                $ export path_value=/home/username/filename$1.class
                $ su username -c 'echo "$path_value"'
                /home/username/filename$1.class


                This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su should do that, but sudo might.



                If we know that the variable can't contain single quotes², then just single-quoting the expanded string in the inner shell would do:



                $ path_value=/home/username/filename$1.class
                $ su username -c "echo '$path_value'"
                /home/username/filename$1.class


                (Note that the quotes are in the opposite order.)





                ¹ if username's login shell is csh or tcsh, replace echo "$path_value" with echo $path_value:q; if it's rc or derivative, with echo $path_value.




                ² and newline characters if the user's login shell is csh or tcsh, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash. Also beware that arguments starting with - or containing backslashes would be a problem with echo, it's better to use printf '%sn' for arbitrary data






                share|improve this answer



















                • 2




                  With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
                  – Stéphane Chazelas
                  Jun 27 at 7:47












                up vote
                3
                down vote










                up vote
                3
                down vote









                Export the value through the environment, then have the inner shell expand it. Assuming username's login shell is Bourne-like¹:



                $ export path_value=/home/username/filename$1.class
                $ su username -c 'echo "$path_value"'
                /home/username/filename$1.class


                This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su should do that, but sudo might.



                If we know that the variable can't contain single quotes², then just single-quoting the expanded string in the inner shell would do:



                $ path_value=/home/username/filename$1.class
                $ su username -c "echo '$path_value'"
                /home/username/filename$1.class


                (Note that the quotes are in the opposite order.)





                ¹ if username's login shell is csh or tcsh, replace echo "$path_value" with echo $path_value:q; if it's rc or derivative, with echo $path_value.




                ² and newline characters if the user's login shell is csh or tcsh, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash. Also beware that arguments starting with - or containing backslashes would be a problem with echo, it's better to use printf '%sn' for arbitrary data






                share|improve this answer















                Export the value through the environment, then have the inner shell expand it. Assuming username's login shell is Bourne-like¹:



                $ export path_value=/home/username/filename$1.class
                $ su username -c 'echo "$path_value"'
                /home/username/filename$1.class


                This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su should do that, but sudo might.



                If we know that the variable can't contain single quotes², then just single-quoting the expanded string in the inner shell would do:



                $ path_value=/home/username/filename$1.class
                $ su username -c "echo '$path_value'"
                /home/username/filename$1.class


                (Note that the quotes are in the opposite order.)





                ¹ if username's login shell is csh or tcsh, replace echo "$path_value" with echo $path_value:q; if it's rc or derivative, with echo $path_value.




                ² and newline characters if the user's login shell is csh or tcsh, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash. Also beware that arguments starting with - or containing backslashes would be a problem with echo, it's better to use printf '%sn' for arbitrary data







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jun 27 at 9:22









                Stéphane Chazelas

                278k52513844




                278k52513844











                answered Jun 27 at 7:20









                ilkkachu

                47.3k668130




                47.3k668130







                • 2




                  With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
                  – Stéphane Chazelas
                  Jun 27 at 7:47












                • 2




                  With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
                  – Stéphane Chazelas
                  Jun 27 at 7:47







                2




                2




                With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
                – Stéphane Chazelas
                Jun 27 at 7:47




                With some su implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
                – Stéphane Chazelas
                Jun 27 at 7:47












                up vote
                1
                down vote













                Why not use sudo instead of su?



                # path_value=/home/username/filename$1.class
                # echo "$path_value"
                /home/username/filename$1.class
                # sudo -u username echo "$path_value"
                /home/username/filename$1.class





                share|improve this answer



























                  up vote
                  1
                  down vote













                  Why not use sudo instead of su?



                  # path_value=/home/username/filename$1.class
                  # echo "$path_value"
                  /home/username/filename$1.class
                  # sudo -u username echo "$path_value"
                  /home/username/filename$1.class





                  share|improve this answer

























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Why not use sudo instead of su?



                    # path_value=/home/username/filename$1.class
                    # echo "$path_value"
                    /home/username/filename$1.class
                    # sudo -u username echo "$path_value"
                    /home/username/filename$1.class





                    share|improve this answer















                    Why not use sudo instead of su?



                    # path_value=/home/username/filename$1.class
                    # echo "$path_value"
                    /home/username/filename$1.class
                    # sudo -u username echo "$path_value"
                    /home/username/filename$1.class






                    share|improve this answer















                    share|improve this answer



                    share|improve this answer








                    edited Jun 27 at 9:24









                    Stéphane Chazelas

                    278k52513844




                    278k52513844











                    answered Jun 27 at 5:03









                    Deathgrip

                    1,266311




                    1,266311




















                        up vote
                        0
                        down vote













                        in the second echo put your variable in single quotes - like:



                        su username -c "echo '$path_value'"


                        That way echo interprets the content of the variable as a string and the "$"-sign is escaped.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          in the second echo put your variable in single quotes - like:



                          su username -c "echo '$path_value'"


                          That way echo interprets the content of the variable as a string and the "$"-sign is escaped.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            in the second echo put your variable in single quotes - like:



                            su username -c "echo '$path_value'"


                            That way echo interprets the content of the variable as a string and the "$"-sign is escaped.






                            share|improve this answer













                            in the second echo put your variable in single quotes - like:



                            su username -c "echo '$path_value'"


                            That way echo interprets the content of the variable as a string and the "$"-sign is escaped.







                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered Jun 27 at 4:59









                            rohr

                            512




                            512




















                                up vote
                                0
                                down vote













                                Remember to properly quote all expansions. The shell will not peek inside single quoted strings.



                                path_value='/home/username/filename$1.class'
                                printf '%sn' "$path_value"


                                Outputs /home/username/filename$1.class.



                                su username -c "printf '%sn' '$path_value'"


                                Identical output.



                                In the second command, the current shell will expand the value of path_value inside tho command, as it is double quoted. The inner printf will then print the single quoted string.



                                Related:



                                • Security implications of forgetting to quote a variable in bash/POSIX shells

                                • When is double-quoting necessary?

                                • Why is printf better than echo?


                                Note that $variable is exactly the same as $variable. Adding ... does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex".






                                share|improve this answer



























                                  up vote
                                  0
                                  down vote













                                  Remember to properly quote all expansions. The shell will not peek inside single quoted strings.



                                  path_value='/home/username/filename$1.class'
                                  printf '%sn' "$path_value"


                                  Outputs /home/username/filename$1.class.



                                  su username -c "printf '%sn' '$path_value'"


                                  Identical output.



                                  In the second command, the current shell will expand the value of path_value inside tho command, as it is double quoted. The inner printf will then print the single quoted string.



                                  Related:



                                  • Security implications of forgetting to quote a variable in bash/POSIX shells

                                  • When is double-quoting necessary?

                                  • Why is printf better than echo?


                                  Note that $variable is exactly the same as $variable. Adding ... does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex".






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    Remember to properly quote all expansions. The shell will not peek inside single quoted strings.



                                    path_value='/home/username/filename$1.class'
                                    printf '%sn' "$path_value"


                                    Outputs /home/username/filename$1.class.



                                    su username -c "printf '%sn' '$path_value'"


                                    Identical output.



                                    In the second command, the current shell will expand the value of path_value inside tho command, as it is double quoted. The inner printf will then print the single quoted string.



                                    Related:



                                    • Security implications of forgetting to quote a variable in bash/POSIX shells

                                    • When is double-quoting necessary?

                                    • Why is printf better than echo?


                                    Note that $variable is exactly the same as $variable. Adding ... does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex".






                                    share|improve this answer















                                    Remember to properly quote all expansions. The shell will not peek inside single quoted strings.



                                    path_value='/home/username/filename$1.class'
                                    printf '%sn' "$path_value"


                                    Outputs /home/username/filename$1.class.



                                    su username -c "printf '%sn' '$path_value'"


                                    Identical output.



                                    In the second command, the current shell will expand the value of path_value inside tho command, as it is double quoted. The inner printf will then print the single quoted string.



                                    Related:



                                    • Security implications of forgetting to quote a variable in bash/POSIX shells

                                    • When is double-quoting necessary?

                                    • Why is printf better than echo?


                                    Note that $variable is exactly the same as $variable. Adding ... does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex".







                                    share|improve this answer















                                    share|improve this answer



                                    share|improve this answer








                                    edited Jun 27 at 7:10


























                                    answered Jun 27 at 7:01









                                    Kusalananda

                                    101k13199312




                                    101k13199312






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452134%2fto-be-able-to-use-a-variable-with-in-its-value-as-current-user-as-well-as-some%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay