0

I used to do bash programming long, long time ago but over a period of time I moved onto other programming languages. My work demands I look into a bash script and I found this piece of code which I am trying to understand.

for var in $(echo $ENV_VARS | cat my_env_vars.txt ); do export $var done

I cannot seem to find much examples where 'cat' command being the pipe target so I am unable to understand the original author's intent in piping the result of echo command onto the output of 'cat' command. Can somebody help explain what is going on?

I did try running the code by setting the ENV_VARS but I see that only the contents of 'my_env_vars.txt' file is being evaluated inside the 'for' loop. So what is the use of piping the result of 'echo' here?

1
  • 2
    By itself, that echo does nothing, but the variable name is interesting when taken with the file name. It seems that the | should be ; which means "take both the ENV_VARS variable & the my_env_vars.txt file to generate a list of variables to export" where the ENV_VARS variable is "Dynamic" & is available only at execution time while the my_env_vars.txt file contains the "Static" normal list of known exports. You should look into the variable contents to make sense of the mistake here ; You should also look into the script activity a little more. Typo mistake made ; to | . . .
    – Prem
    Commented Sep 18, 2022 at 10:13

1 Answer 1

1

Try:

echo $ENV_VARS | cat - my_env_vars.txt

The - as the first argument instructs cat to read its standard input as the first file (and write the data to its standard output, then any filenames on the command line are read (and written to its standard output) in the sequence they appear on the command line.

The - argument can appear after my_env_vars.txt as well. (However, while naming a file on the command line multiple times can make cat open and read the file multiple times, this isn't true for stdandard input (-))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .