When root, source another user's ~/.bashrc and get all environment variablesHow to set environment variables permanently for one userWhat is the difference between ./script and . ./script?Any other way to define an environment variable?How to source correct startup scripts on interactive, non-login shellAre environment variables visible to unprivileged users on Linux?How to prevent the caller's shell from being used in sudoWant to preserve bashrc and vimrc when switching to non-root usersQuestion about global environment variables and fork() & exec()Order of scripts run before bash startsHow do I use ssh-agent as a wrapper program?

Calculate Levenshtein distance between two strings in Python

What is GPS' 19 year rollover and does it present a cybersecurity issue?

Is domain driven design an anti-SQL pattern?

Doomsday-clock for my fantasy planet

Why doesn't a const reference extend the life of a temporary object passed via a function?

What do the Banks children have against barley water?

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

Denied boarding due to overcrowding, Sparpreis ticket. What are my rights?

Is Social Media Science Fiction?

Filling an area between two curves

Domain expired, GoDaddy holds it and is asking more money

Why is my log file so massive? 22gb. I am running log backups

Why airport relocation isn't done gradually?

Copycat chess is back

Is a vector space a subspace of itself?

COUNT(*) or MAX(id) - which is faster?

A poker game description that does not feel gimmicky

How to manage monthly salary

Are cabin dividers used to "hide" the flex of the airplane?

Manga about a female worker who got dragged into another world together with this high school girl and she was just told she's not needed anymore

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

I see my dog run

Are objects structures and/or vice versa?

Landing in very high winds



When root, source another user's ~/.bashrc and get all environment variables


How to set environment variables permanently for one userWhat is the difference between ./script and . ./script?Any other way to define an environment variable?How to source correct startup scripts on interactive, non-login shellAre environment variables visible to unprivileged users on Linux?How to prevent the caller's shell from being used in sudoWant to preserve bashrc and vimrc when switching to non-root usersQuestion about global environment variables and fork() & exec()Order of scripts run before bash startsHow do I use ssh-agent as a wrapper program?






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








0















I'm trying to develop a small system app that is run as root.
In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



What I'm trying to accomplish is equivalent to:



  • Change from root to user

  • source ~/.bashrc or ~/.profile etc.

  • printenv [and get it after the script]

  • Change to root again

There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



Any guidance is appreciated,



In case of missing information, please let me know.



Kind regards,










share|improve this question




























    0















    I'm trying to develop a small system app that is run as root.
    In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



    What I'm trying to accomplish is equivalent to:



    • Change from root to user

    • source ~/.bashrc or ~/.profile etc.

    • printenv [and get it after the script]

    • Change to root again

    There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



    I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



    sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


    Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



    Any guidance is appreciated,



    In case of missing information, please let me know.



    Kind regards,










    share|improve this question
























      0












      0








      0








      I'm trying to develop a small system app that is run as root.
      In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



      What I'm trying to accomplish is equivalent to:



      • Change from root to user

      • source ~/.bashrc or ~/.profile etc.

      • printenv [and get it after the script]

      • Change to root again

      There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



      I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



      sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


      Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



      Any guidance is appreciated,



      In case of missing information, please let me know.



      Kind regards,










      share|improve this question














      I'm trying to develop a small system app that is run as root.
      In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



      What I'm trying to accomplish is equivalent to:



      • Change from root to user

      • source ~/.bashrc or ~/.profile etc.

      • printenv [and get it after the script]

      • Change to root again

      There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



      I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



      sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


      Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



      Any guidance is appreciated,



      In case of missing information, please let me know.



      Kind regards,







      linux bash shell-script sudo environment-variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 7 '18 at 18:06









      mozcelikorsmozcelikors

      1468




      1468




















          1 Answer
          1






          active

          oldest

          votes


















          0














          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer























          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

            – mozcelikors
            Feb 7 '18 at 18:45











          • I've tested it on CentOS.

            – Kevin Lemaire
            Feb 7 '18 at 18:46











          • If you su - user1 and then printenv, do you see the env variables you want?

            – Kevin Lemaire
            Feb 7 '18 at 18:47











          • Yes, that works. If I printenv when I'm user. Then, it works.

            – mozcelikors
            Feb 7 '18 at 18:48











          • But I need a one-liner that involves environment refresh

            – mozcelikors
            Feb 7 '18 at 18:49











          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%2f422610%2fwhen-root-source-another-users-bashrc-and-get-all-environment-variables%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer























          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

            – mozcelikors
            Feb 7 '18 at 18:45











          • I've tested it on CentOS.

            – Kevin Lemaire
            Feb 7 '18 at 18:46











          • If you su - user1 and then printenv, do you see the env variables you want?

            – Kevin Lemaire
            Feb 7 '18 at 18:47











          • Yes, that works. If I printenv when I'm user. Then, it works.

            – mozcelikors
            Feb 7 '18 at 18:48











          • But I need a one-liner that involves environment refresh

            – mozcelikors
            Feb 7 '18 at 18:49















          0














          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer























          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

            – mozcelikors
            Feb 7 '18 at 18:45











          • I've tested it on CentOS.

            – Kevin Lemaire
            Feb 7 '18 at 18:46











          • If you su - user1 and then printenv, do you see the env variables you want?

            – Kevin Lemaire
            Feb 7 '18 at 18:47











          • Yes, that works. If I printenv when I'm user. Then, it works.

            – mozcelikors
            Feb 7 '18 at 18:48











          • But I need a one-liner that involves environment refresh

            – mozcelikors
            Feb 7 '18 at 18:49













          0












          0








          0







          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer













          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 7 '18 at 18:39









          Kevin LemaireKevin Lemaire

          1,181724




          1,181724












          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

            – mozcelikors
            Feb 7 '18 at 18:45











          • I've tested it on CentOS.

            – Kevin Lemaire
            Feb 7 '18 at 18:46











          • If you su - user1 and then printenv, do you see the env variables you want?

            – Kevin Lemaire
            Feb 7 '18 at 18:47











          • Yes, that works. If I printenv when I'm user. Then, it works.

            – mozcelikors
            Feb 7 '18 at 18:48











          • But I need a one-liner that involves environment refresh

            – mozcelikors
            Feb 7 '18 at 18:49

















          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

            – mozcelikors
            Feb 7 '18 at 18:45











          • I've tested it on CentOS.

            – Kevin Lemaire
            Feb 7 '18 at 18:46











          • If you su - user1 and then printenv, do you see the env variables you want?

            – Kevin Lemaire
            Feb 7 '18 at 18:47











          • Yes, that works. If I printenv when I'm user. Then, it works.

            – mozcelikors
            Feb 7 '18 at 18:48











          • But I need a one-liner that involves environment refresh

            – mozcelikors
            Feb 7 '18 at 18:49
















          That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

          – mozcelikors
          Feb 7 '18 at 18:45





          That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc

          – mozcelikors
          Feb 7 '18 at 18:45













          I've tested it on CentOS.

          – Kevin Lemaire
          Feb 7 '18 at 18:46





          I've tested it on CentOS.

          – Kevin Lemaire
          Feb 7 '18 at 18:46













          If you su - user1 and then printenv, do you see the env variables you want?

          – Kevin Lemaire
          Feb 7 '18 at 18:47





          If you su - user1 and then printenv, do you see the env variables you want?

          – Kevin Lemaire
          Feb 7 '18 at 18:47













          Yes, that works. If I printenv when I'm user. Then, it works.

          – mozcelikors
          Feb 7 '18 at 18:48





          Yes, that works. If I printenv when I'm user. Then, it works.

          – mozcelikors
          Feb 7 '18 at 18:48













          But I need a one-liner that involves environment refresh

          – mozcelikors
          Feb 7 '18 at 18:49





          But I need a one-liner that involves environment refresh

          – mozcelikors
          Feb 7 '18 at 18:49

















          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%2f422610%2fwhen-root-source-another-users-bashrc-and-get-all-environment-variables%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

          getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

          Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

          Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.