14

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

kishore
  • 1,658
  • 7
  • 24
  • 33
  • Possible duplicate of [Pass all variables from one shellscript to another?](https://stackoverflow.com/questions/9772036/pass-all-variables-from-one-shellscript-to-another) – l'L'l Jun 05 '17 at 08:26

6 Answers6

22

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
Inian
  • 80,270
  • 14
  • 142
  • 161
15

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)
hal58th
  • 175
  • 1
  • 7
  • This approach works just until spaces appear in variable values, so it didn't work for me. The only solution that could cope with spaces was found here https://unix.stackexchange.com/a/659951 – Cool Soft Aug 30 '22 at 11:54
6

export is a built-in function, and xargs takes executable as an argument, not a function. Also, there is no sense in wrapping export into some script or subshell, xargs won't fail in such case, but your vars won't be exported anyway. So better to choose some another approach, w/o xargs, like showed in the previous comment.

Dieselist
  • 880
  • 6
  • 15
4

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)

Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    Unfortunately, this does not work with values that have spaces. One gets an error from export that reads: `export: not valid in this context` – John Feb 26 '20 at 15:12
0

This will allow filtering or rewriting of the file and then run the export command on each line that is returned

for i in `cat <FileName> | grep " "`; do
  export $i
done
Pete Brumm
  • 1,656
  • 19
  • 13
0

The best one to me is

export $(grep -v '^#' .env | xargs)

This command let you write the .env file and comment lines with #.
So it is compatible with your variable file and with

# This it the host 
HOST=localhost
# This is the port
PORT=8080
BugliL
  • 868
  • 1
  • 9
  • 18