V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
holmesabc
V2EX  ›  Linux

bash 可否通过 bash 脚本来设置当前 shell 的环境变量?

  •  
  •   holmesabc ·
    holmeszyx · Jun 8, 2016 · 6444 views
    This topic created in 3614 days ago, the information mentioned may be changed or developed.
    我想设置 http 的代理.每次都在当前 shell 下输入 export http_proxy=http://127.0.0.1:7777 这类好蛋疼.

    就想一条命令解决. 但是如果写在一个 bash 脚本晨面. 在当前 shell 执行这个脚本的话, 应该只是改了那个脚本里面的环境变量,
    对当前 shell 没什么鸟用.

    不知道是 bash 写的不对,还是什么的.
    还是说只有设置 bashrc. 加个 alias
    Supplement 1  ·  Jun 8, 2016
    临时使用的.

    看来还是要改 bashrc 了.

    @lukertty 的方法,最接近我的需求.

    按其方法 , 在 bashrc 里面加那两个 function.
    也可以再加句 export -f set_proxy


    ok, 基本可以结了.
    多谢大家
    Supplement 2  ·  Jun 8, 2016
    保存了个 http_proxy 文件
    --------------------------------

    ```
    set_http_proxy() {
    export http_proxy=http://127.0.0.1:7777
    export https_proxy=https://127.0.0.1:7777
    }

    unset_http_proxy() {
    unset http_proxy
    unset https_proxy
    }

    export -f set_http_proxy
    export -f unset_http_proxy

    ```
    .bashrc 最后加上
    --------------------------------

    source ~/http_proxy


    shell 里面使用
    ---------------------------

    set_http_proxy
    或者
    unset_http_proxy
    24 replies    2016-06-15 10:43:52 +08:00
    missdeer
        1
    missdeer  
       Jun 8, 2016
    没看懂,~/.bashrc 里加一句 export http_proxy=http://127.0.0.1:7777 不行么?
    holmesabc
        2
    holmesabc  
    OP
       Jun 8, 2016
    @missdeer 不想加个全局, 只是需要的时候临时用. 当临时用又不想每次写这么长的命令
    xiaooloong
        3
    xiaooloong  
       Jun 8, 2016
    #!/bin/sh
    export http_proxy=http://127.0.0.1:7777
    保存为 a.sh

    需要用的时候 source a.sh
    xuboying
        4
    xuboying  
       Jun 8, 2016 via Android
    @xiaooloong source =>.
    xiaooloong
        5
    xiaooloong  
       Jun 8, 2016
    @xuboying 啊??
    Goooogle
        6
    Goooogle  
       Jun 8, 2016
    如果想在一定时间内设置代理,那么 alias 最合适
    设置代理
    alias hp="export http_proxy=http://127.0.0.1:7777"
    清除代理
    alias us="unset http_proxy"

    如果只是特定命令用代理,用 proxychains 吧
    proxychains sudo apt-get update
    wujunze
        7
    wujunze  
       Jun 8, 2016
    楼主是在设置 linux 下 shadowsocks 命令行代理麽?
    lukertty
        8
    lukertty  
       Jun 8, 2016   ❤️ 1
    不懂 lz 到底是需要全局还是不需要全局啊
    全局:
    set_proxy(){
    export http_proxy=http://127.0.0.1:7777
    }
    unset_proxy(){
    unset http_proxy
    }

    只需要一条命令:
    http_proxy=http://127.0.0.1:7777 balabala
    CinderellaCiCi
        9
    CinderellaCiCi  
       Jun 8, 2016
    可以。
    保存到 shell 脚本里面,要用的时候执行下脚本。
    wxg4net
        10
    wxg4net  
       Jun 8, 2016
    为啥不用 proxychains 代理方式呢
    clino
        11
    clino  
       Jun 8, 2016
    proxychains +1
    kfll
        12
    kfll  
       Jun 8, 2016
    bdbai
        13
    bdbai  
       Jun 8, 2016 via Android
    @xiaooloong xuboying 的意思是
    ./a.sh
    jason19659
        14
    jason19659  
       Jun 8, 2016
    source 可行
    imn1
        15
    imn1  
       Jun 8, 2016
    tsocks
    Neveroldmilk
        16
    Neveroldmilk  
       Jun 8, 2016
    临时的设置可以用 Export ,不过一旦注销什么的就废了。另外,翻墙的话不要用 bash ,很多高级功能搞不定的。
    ryd994
        17
    ryd994  
       Jun 8, 2016 via Android   ❤️ 1
    @bdbai 不是
    应该是 . ./a.sh 注意两个点之间的空格
    第一个点等效于 source
    dorentus
        18
    dorentus  
       Jun 8, 2016
    写在 .bashrc 里面:
    alias proxyed = "env http_proxy=http://127.0.0.1:7777 $@"

    用的时候加 proxyed 在任意命令前头:
    proxyed curl http://ipecho.net/plain
    dorentus
        19
    dorentus  
       Jun 8, 2016
    @dorentus man env 看详情
    est
        20
    est  
       Jun 8, 2016
    bash 真是弱爆了。执行一条命令,保持当前 shell 不退出,这个功能居然他妈做不到。有 hack 居然是用 bash_rc 去 hack 。。。

    windows 下 cmd /k 这种。
    mengzhuo
        21
    mengzhuo  
       Jun 8, 2016 via iPhone
    最快的还是 ctrl r 搜索 特别是 lz 这种
    ctrl r 77 估计都出来了
    SoloCompany
        22
    SoloCompany  
       Jun 8, 2016
    source 和 . 是正解
    detailyang
        23
    detailyang  
       Jun 9, 2016
    子进程怎么能影响父进程的变量呢 科科
    messyidea
        24
    messyidea  
       Jun 15, 2016
    发现 zsh 不支持 export -f 。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2270 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 01:21 · PVG 09:21 · LAX 18:21 · JFK 21:21
    ♥ Do have faith in what you're doing.