Setting environment variable globally without restarting Ubuntu

前端 未结 3 2032
暗喜
暗喜 2021-01-30 11:37

I know that system wide environment variables can be set by adding entries into

/etc/environment

or

/etc/profile
3条回答
  •  死守一世寂寞
    2021-01-30 11:56

    This perl program uses gdb to change the USER variable in all currently running bash shells to whatever is given as the program arg. To set a new variable the internal bash call "set_if_not" could be used

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my @pids = qx(ps -C bash -o pid=);
    my $foo  = $ARGV[0];
    print "changing user to $foo";
    print @pids;
    
    open( my $gdb, "|gdb" ) || die "$! gdb";
    select($gdb);
    $|++;
    for my $pid ( @pids ) {
        print "attach $pid\n";
        sleep 1;
        print 'call bind_variable("USER","' . $foo . '",0)' . "\n";
        sleep 1;
        print "detach\n";
    }
    

    This only works with bash ( I only tested it with version 4.1 on Ubuntu 10.04 LTS) and does not alter the environment for arbitrary already running programs. Obviously it must be run as root.

提交回复
热议问题