How to write environment variables between shell sections preferably without using a script

0

I use several shell sections, with different parameters to compile applications for multiple platforms, sometimes need to close a shell and open it again for some reason, a crash or another reason.

I use scripts to keep the environment variables parameterized as accurate, but sometimes I change a variable so I have to create a new script, just for that moment which makes it a bit inconvenient.

Shell, in particular Bash, has some feature that I can export the variables between sections so I do not have to create scripts? Can I close the shell and import a set of variables according to a profile I want?

    
asked by anonymous 04.11.2016 / 13:54

2 answers

0

Have you tried the command export ?

Set the ~/.bashrc command to the /etc/profile or export or (the second depends on your distro) followed by the variable receiving the value.

In case you forget the running shell and someone else get logged in ... (: D)

Edit the /etc/profile in this case and place at the end of the file:

TMOUT=300
export TMOUT

To be safer, put readonly so you can not change your variable ...

readonly TMOUT=300
export TMOUT

I used this example to try to illustrate, so your sessions will be logged off if you spend 5 minutes without interaction.

VARIAVEL=VALOR
export VARIAVEL
    
30.10.2017 / 01:48
0

Doing this completely "no script" is not possible: each new bash shell (other than a "pre-existing" shell) is "zeroed" and preconfigured by .profile , .bashrc , moment of login. Daughter shells inherit all exports executed in the parent shell.

That is, you need to have a place to store the various variables and their values that you want to use in the future. I particularly like doing this sort of thing using aliases prefixed in my .profile or .bash_aliases , in the following format:

$ alias exportABC='export a=1 b=2 c=3'
$ exportABC 
$ echo $a $b $c
1 2 3

The advantage, in my view, is that you have all your standard exports within reach of any shell and with the readline autocompletion on the TAB key. You can give a default export to each compile target environment, for example.

The downside is that things can get pretty confusing pretty fast if the variables exported by each alias have different names, you would end up with a gigantic and unintelligible env . It's always good to have a "cleaner" of exports for all the variables you use, making it easy to start over from scratch if necessary:

$ alias exportClean='unset a b c'
$ exportClean 
$ echo $a $b $c
(linha vazia)
    
02.11.2017 / 16:10