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

  • SEARCH
  • Home
  • 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 8118061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:22:07+00:00 2026-06-06T04:22:07+00:00

If I run $#/bin/bash for i in `seq 5`; do exec 3> >(sed -e

  • 0

If I run

$#/bin/bash
for i in `seq 5`; do
    exec 3> >(sed -e "s/^/$i: /"; echo "$i-")
    echo foo >&3
    echo bar >&3
    exec 3>&-
done

then the result is not synchronous; it could be something like:

1: foo
1: bar
2: foo
2: bar
1-
3: foo
3: bar
2-
3-
4: foo
5: foo
4: bar
5: bar
4-
5-

How do I ensure that the process substitution >(...) is completed before proceeding to the next iteration?

Inserting sleep 0.1 after exec 3>&- helped, but it’s inelegant, inefficient, and not guaranteed to always work.

EDIT: The example may look silly, but it was for illustration only. What I’m doing is reading a stream of input in a loop, feeding each line to a process which occasionally changes during the loop. Easier explained in code:

# again, simplified for illustration
while IFS= read line; do
    case $line in
    @*)
        exec 3>&-
        filename=${line:1}
        echo "starting $filename"
        exec 3> >(sort >"$filename"; echo "finished $filename")
        ;;
    *)
        echo "$line" >&3
        ;;
    esac
done
exec 3>&-
  • 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-06-06T04:22:09+00:00Added an answer on June 6, 2026 at 4:22 am

    The following works in bash 4, using coprocesses:

    #!/bin/bash
    fd_re='^[0-9]+$'
    cleanup_and_wait() {
        if [[ ${COPROC[1]} =~ $fd_re ]] ; then
            eval "exec ${COPROC[1]}<&-"
            echo "waiting for $filename to finish" >&2
            wait $COPROC_PID
        fi
    }
    
    while IFS= read -r line; do
        case $line in
        @*)
            cleanup_and_wait
            filename=${line:1}
            echo "starting $filename" >&2
            coproc { sort >"$filename"; echo "Finished with $filename" >&2; }
            ;;
        *)
            printf '%s\n' "$line" >&${COPROC[1]}
            ;;
        esac
    done
    cleanup_and_wait
    

    For prior versions of bash, a named pipe can be used instead:

    cleanup_and_wait() {
        if [[ $child_pid ]] ; then
          exec 4<&-
          echo "waiting for $filename to finish" >&2
          wait $child_pid
        fi
    }
    
    # this is a bit racy; without a force option to mkfifo,
    # however, the race is unavoidable
    fifo_name=$(mktemp -u -t fifo.XXXXXX)
    if ! mkfifo "$fifo_name" ; then
      echo "Someone else may have created our temporary FIFO before we did!" >&2
      echo "This can indicate an attempt to exploit a race condition as a" >&2
      echo "security vulnarability and should always be tested for." >&2
      exit 1
    fi
    
    # ensure that we clean up even on unexpected exits
    trap 'rm -f "$fifo_name"' EXIT
    
    while IFS= read -r line; do
        case $line in
        @*)
            cleanup_and_wait
            filename=${line:1}
            echo "starting $filename" >&2
            { sort >"$filename"; echo "finished with $filename" >&2; } <"$fifo_name" &
            child_pid=$!
            exec 4>"$fifo_name"
            ;;
        *)
            printf '%s\n' "$line" >&4
            ;;
        esac
    done
    cleanup_and_wait
    

    A few notes:

    • It’s safer to use printf '%s\n' "$line" than echo "$line"; if a line contains only -e, for instance, some versions of echo will do nothing with it.
    • Using an EXIT trap for cleanup ensures that an unexpected SIGTERM or other error won’t leave the stale fifo sitting around.
    • If your platform provides a way to create a FIFO with an unknown name in a single, atomic operation, use it; this would avoid the condition that requires us to always test whether the mkfifo is successful.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code: #!/bin/bash for i in $@; do echo $i; done; run script: #
Script test.sh: #!/bin/bash if [[ $# -eq 0 ]]; then echo no arg else
my script= #!/bin/bash echo ************************BEGIN LOG******************************>>/root/backup_scripts/new_scripts/vmbackup.log 2>&1 date +%m/%d/%Y %H:%M:%S $HOSTNAME>>/root/backup_scripts/new_scripts/vmbackup.log 2>&1 ruby /root/backup_scripts/new_scripts/aapxen01.rb>>/root/backup_scripts/new_scripts/vmbackup.log
W/hen i run the following code snippet #!/bin/bash if [ foo = foo ];
Here's my tiny script to run celeryd: #!/bin/bash -x LOG=/home/var/log/ingest.log sudo -u apache bash
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I am using execv() to run commands from /bin/ such as 'ls', 'pwd', 'echo'
I want to write a bash script where I run two commands simultaneously, then
It is not totally rigth that I can run a Bash function with 000
Here is the script that I want to run using git filter-branch : #!/bin/bash

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.