Unix & Linux: How to export variables from a file? (4 Solutions!!)

preview_player
Показать описание
Unix & Linux: How to export variables from a file?

a=123
b="hello world"
c="one more variable"
How can I export all these variables using the export command, so that they can
later be used by child processes?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 82 people ==
export a b c
./child ...
----
Judging by your other question, you don't want to hardcode the variable names:
test it:
$ echo "$a $b $c"
123 hello world one more variable

123 hello world one more variable

== This solution helped 215 people ==
set -a
set +a
set -a causes variables1 defined from now on to be automatically exported. It's
available in any Bourne-like shell. . is the standard and Bourne name for the
source command so I prefer it for portability (source comes from csh and is now
available in most modern Bourne-like shells including bash though (sometimes
with a slightly different behaviour)).
In POSIX shells, you can also use set -o allexport as a more descriptive
alternative way to write it (set +o allexport to unset).
----
1 In bash, beware that it also causes all functions declared while allexport is
on to be exported to the environment (as BASH_FUNC_myfunction%% environment
variables that are then imported by all bash shells run in that environment,
even when running as sh).

== This solution helped 1 person ==
Just do:

Рекомендации по теме
visit shbcf.ru