I have a file which looks as below.
HOST=localhost
PORT=8080
I want to export the above into environment. I am executing the following command to export the variables in file to environment.
cat <FileName> | xargs export
I am getting the following exception when trying to run the above command.
xargs: export: No such file or directory
Why use xargs
? When you can just source
the file in the current shell
You can use the set
builtin, along with the export
instead of using a non-shell built-in like xargs
. Just do below
set -o allexport
. ./file_containing_variables
set +o allexport
The usage of allexport
flag with you set(-o)/unset(+o) allows you to export variables directly from the command-line. The second line is the POSIX
source
(dot followed by variable name) command to reflect all variables to the current shell and within the allexport
flag, it is made available permanently.
Refer GNU set
built-in page for more details.
Another way in bash
using input re-direction on the file
while IFS= read -r line; do
export "$line"
done <file_containing_variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With