How to keep environment variables when using sudo

前端 未结 6 818
滥情空心
滥情空心 2020-11-22 01:55

When I use any command with sudo the environment variables are not there. For example after setting HTTP_PROXY the command wget works fine without sudo

6条回答
  •  日久生厌
    2020-11-22 02:43

    If you have the need to keep the environment variables in a script you can put your command in a here document like this. Especially if you have lots of variables to set things look tidy this way.

    # prepare a script e.g. for running maven
    runmaven=/tmp/runmaven$$
    # create the script with a here document 
    cat << EOF > $runmaven
    #!/bin/bash
    # run the maven clean with environment variables set
    export ANT_HOME=/usr/share/ant
    export MAKEFLAGS=-j4
    mvn clean install
    EOF
    # make the script executable
    chmod +x $runmaven
    # run it
    sudo $runmaven
    # remove it or comment out to keep
    rm $runmaven
    

提交回复
热议问题