Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
holmesabc
V2EX  ›  Linux

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

  •  
  •   holmesabc ·
    holmeszyx · Jun 8, 2016 · 6926 views
    This topic created in 3825 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
    missdeer
    Jun 8, 2016
    1
    没看懂,~/.bashrc 里加一句 export http_proxy=http://127.0.0.1:7777 不行么?
    holmesabc
    holmesabc
    OP
    Jun 8, 2016
    2
    @missdeer 不想加个全局, 只是需要的时候临时用. 当临时用又不想每次写这么长的命令
    xiaooloong
    xiaooloong
    Jun 8, 2016
    3
    #!/bin/sh
    export http_proxy=http://127.0.0.1:7777
    保存为 a.sh

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

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

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

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