Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs with export is not working

Tags:

bash

xargs

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

like image 635
kishore Avatar asked Jun 05 '17 07:06

kishore


1 Answers

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
like image 173
Inian Avatar answered Oct 02 '22 14:10

Inian