CodexBloom - Programming Q&A Platform

advanced patterns when using Bash process substitution with `curl` and `grep`

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-08-24
bash curl grep process-substitution

After trying multiple solutions online, I still can't figure this out. I'm stuck on something that should probably be simple. I'm trying to fetch some data from an API using `curl`, and I want to filter the output with `grep`. I'm attempting to do this all in one line using process substitution, but I'm running into some unexpected behavior. Here’s the command I’m using: ```bash curl -s https://api.example.com/data | grep "pattern" > >(tee output.txt) ``` The intention is to save the filtered results into `output.txt` while still displaying them in the terminal. However, it seems that `output.txt` is empty after the command finishes executing, and nothing gets written to it. I even tried using `set -x` to debug, and I didn’t see any errors. The command runs successfully, but the output.txt file remains empty. I’ve also verified that the pattern I’m searching for exists in the output from the `curl` command. To further troubleshoot, I changed the command to: ```bash curl -s https://api.example.com/data | tee >(grep "pattern") > output.txt ``` But now `output.txt` includes all the data fetched from the API instead of just the filtered results. I suspect there’s something about how I’m utilizing process substitution that’s causing this scenario. I’m running Bash version 5.1 on Ubuntu 20.04. Could anyone guide to understand what I might be doing wrong or suggest a better way to achieve this? I've been using Bash for about a year now. Any feedback is welcome!