Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:52+00:00 2026-05-26T01:14:52+00:00

I need to read some configuration data into environment variables in a bash script.

  • 0

I need to read some configuration data into environment variables in a bash script.

The “obvious” (but incorrect) pattern is:

egrep "pattern" config-file.cfg | read VAR1 VAR2 VAR3 etc...

This fails because the read is run in a subshell and therefore cannot set variables in the invoking shell. So I came up with this as an alternative

coproc egrep "pattern" config-file.cfg
read -u ${COPROC[0]} VAR1 VAR2 VAR3 etc...

which works fine.

To test what happens if the coprocess returns more than one line, I tried this:

coproc cat config-file.cfg
read -u ${COPROC[0]} VAR1 VAR2 VAR3 etc...

where config-file.cfg contains three lines.

$ cat config-file.cfg
LINE1 A1 B1 C1
LINE2 A2 B2 C2
LINE3 A3 B3 C3

I expected this to process the first line in the file, followed by some kind of “broken pipe” error message. While it did process the first line, there was no error message and no coprocess was left running.

So I then tried the following in a script:

$ cat test.sh
coproc cat config-file.cfg
read -u ${COPROC[0]} VAR1 VAR2 VAR3 VAR4
echo $VAR1 $VAR2 $VAR3 $VAR4
wait
echo $?

Running it:

$ bash -x test.sh
+ read -u 63 VAR1 VAR2 VAR3 VAR4
+ cat config-file.cfg
LINE1 A1 B1 C1
+ wait
+ echo 0
0

Where did the remaining two lines go? I would have expected either “broken pipe”, or the wait to hang since there was nothing to read the remaining lines, but as you can see the return code was zero.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:14:53+00:00Added an answer on May 26, 2026 at 1:14 am

    As per comments above, you can use process substitution to achieve just that. This way, read is not run in a subshell and the captured vars will be available within the current shell.

    read VAR1 VAR2 VAR3 < <(egrep "pattern" config-file.cfg)
    

    “If the <(list) form is used, the file passed as an argument should be read to obtain the output of list” — what “file passed as an agrument” are they talking about?

    That is rather cryptic to me too. The chapter on process substitution in Advanced Bash-scripting Guide has a more comprehensive explanation.

    The way I see it, when the <(cmd) syntax is used, the ouput of cmd is made available via a named pipe (or temp file) and the syntax is replaced by the filename of the pipe/file. So for the example above, it would end up being equivalent to:

    read VAR1 VAR2 VAR3 < /dev/fd/63
    

    where /dev/fd/63 is the named pipe connected to the stdout of cmd.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read the SDK document, but can not understand some details, need some help.
I have some configuration data in a config file that I read off disk
for some test I need to run a data driven test with a configuration
I need to read some large files (from 50k to 100k lines), structured in
I need to read from a variety of different text files (I've some delimited
I need some help ... I'm a bit (read total) n00b when it comes
I need to read data added to the end of an executable from within
I'm using SQLBULKCOPY to copy some data-tables into a database table, however, because the
I've read some tutorials on Python metaclasses. I've never used one before, but I
Please forgive my attempts at necromancy, but I actually need to write some code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.