D
D
dmitriy2016-10-04 23:07:33
bash
dmitriy, 2016-10-04 23:07:33

How to set global variables?

the .env file contains an environment variable (one for example):
HOST=localhost:3336
the set_env.sh script "should" make the variables global:

#!/bin/bash
source .env && export $(cut -d= -f1 .env)

but when executing the command
$ ./set_env.sh
$ echo $HOST
nothing is output, the question is how to set a global variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-10-05
@dmitriylanets

$ cat env_file

HOST=foo

$ export `cat env_file`
or
$ env `cat env_file`

R
RPG, 2016-10-04
@RPG

The only way:

source .env
echo $HOST

This can be thrown into .bashrc, for example. Depends on the task. export will work inside the shell, it doesn’t work for you because another bash is started inside and the variable is already set there. You cannot influence the environment of the parent process (or any other process).
Also, I don't recommend using an already existing variable ($HOST contains the name of the current host).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question