xargs with export is not working

Viewed 10816

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

4 Answers

It is possible to use xargs but you have to execute it as a sub-shell and use the -L 1 option to pass back one line at a time.

export $(cat FileName | xargs -L 1)

This approach would allow you to do further string manipulation if you needed. For example you can remove spaces from the file.

export $(cat FileName | tr -d ' ' | xargs -L 1)

Dieselist correctly explained the reasons why the original approach did not work. The approaches presented by Inian shall work, but a simpler way is (cf. Command Substitution):

export $(<FileName)

Related