Skip to content
Go back

xargs vs while read: Which One and When

By KingPin 6 min read
xargs vs while read: Which One and When

The Core Difference

Use xargs when you’re piping thousands of arguments into one command, and use while read when each line needs its own logic or field parsing.

Both xargs and while read iterate over input and run commands. But they solve slightly different problems, and picking the wrong one makes your scripts slower, fragile, or both.

while read is a bash loop. xargs is a tool that builds command lines. They’re fundamentally different approaches to the same problem.

Which Loop Should Handle Your Shell Script?

while readxargs
Speed on 1000 itemsBaseline, spawns per line10 to 50x faster
ParallelismNoYes, with -P
Variable persistenceBroken by the subshellN/A, no loop body
Handles spaces safelyYes, by defaultNeeds -print0 / -0
Best forPer-line logic, CSV parsingBulk argument passing

while read: Simple, Slow, Safe(ish)

while read reads stdin line by line and runs a command on each. It’s straightforward:

Terminal window
cat file.txt | while read line; do
echo "Processing: $line"
done

Pros:

Cons:

Here’s where the subshell bites you:

Terminal window
count=0
ls -1 | while read file; do
((count++))
done
echo $count # Prints 0, not the actual count

The count increments inside the loop subshell, but outside? It’s still zero.

xargs: Fast, Parallel, Overkill Sometimes

xargs takes stdin and builds command-line arguments. Instead of looping, it bunches inputs together and runs the command fewer times:

Terminal window
find . -name "*.log" | xargs rm

That’s not a loop, it’s one or a few rm invocations with lots of arguments.

Pros:

Cons:

Safe xargs with find:

Terminal window
find . -name "*.log" -print0 | xargs -0 rm

The -print0 and -0 pair means: separate with null bytes instead of newlines. Handles filenames with spaces correctly.

Real Examples

Batch Download URLs (xargs wins)

Terminal window
cat urls.txt | xargs -I {} -P 4 curl -O {}

Here -P 4 means run 4 parallel curl jobs. With while read, you’d spawn a curl per iteration, way slower.

Process CSV Rows (while read is clearer)

Terminal window
while IFS=',' read id name email; do
echo "INSERT INTO users VALUES ($id, '$name', '$email')" | mysql db
done < data.csv

You need the field parsing anyway. xargs would be awkward here because each row becomes one command.

Count Files in Subdirs (while read, fix the subshell)

Terminal window
count=0
find . -mindepth 1 -maxdepth 1 -type d | while read dir; do
((count += $(ls "$dir" | wc -l)))
done
echo $count

Wait, subshell problem again. Fix it:

Terminal window
find . -mindepth 1 -maxdepth 1 -type d | while read dir; do
ls "$dir" | wc -l
done | awk '{sum += $1} END {print sum}'

Now the loop outputs numbers, and awk sums them outside the subshell.

The Decision Tree

Use while read if:

Use xargs if:

Bonus: Use GNU parallel if xargs is too basic:

Terminal window
cat urls.txt | parallel -j 4 curl -O {}

It’s xargs on steroids, better error handling, easier syntax, way more flexible.

The mapfile Alternative

If you’re using while read to populate an array, there’s a better way:

Terminal window
# Instead of this:
while read line; do
my_array+=("$line")
done < file.txt
# Use mapfile (bash 4+):
mapfile -t my_array < file.txt

mapfile reads everything into an array without a loop. No subshell issues. No IFS weirdness. Way faster.

Then iterate the array:

Terminal window
for item in "${my_array[@]}"; do
echo "Processing: $item"
done

find -exec vs xargs

find -exec is xargs with a different syntax:

Terminal window
# xargs
find . -name "*.log" | xargs rm
# find -exec (one command per file)
find . -name "*.log" -exec rm {} \;
# find -exec (batched, like xargs)
find . -name "*.log" -exec rm {} +

The + form batches files into one rm call, same behavior as xargs, but null-safe by default. Use it when you’re already in a find command and don’t want to pipe.

The Speed Difference

Real talk: on a list of 1000 files, xargs is 10 to 50x faster than while read. If you’re processing thousands of items, spend 2 minutes learning xargs -0. Your scripts will thank you.

But if you’re processing 50 lines? Readability wins. Go with while read.

Common Questions

Why is my variable still 0 after a while read loop?

Variables set inside a piped while read loop vanish because the pipe runs the loop in a subshell. The count increments there, and the parent shell still sees 0. Fix it by having the loop print one number per iteration and summing the output with awk outside the subshell. To fill an array, skip the loop and use mapfile -t, which has no subshell problem.

Is find -exec with a plus sign as fast as xargs?

Yes, in the way that matters. The + form batches files into one rm call, the same behavior as xargs, and it is null-safe by default. The \; form runs one command per file instead. Use the + form when you’re already inside a find command and don’t want to pipe.

How do I use xargs with filenames that contain spaces?

Pair find -print0 with xargs -0. Both sides then separate names with null bytes instead of newlines, so spaces stop splitting one filename into several arguments. Plain xargs has trouble with spaces and special characters.

Does mapfile work in older versions of bash?

No. mapfile -t my_array < file.txt needs bash 4 or newer. On older versions, use a while read loop that appends each line to the array.


Wrote about this post on your own site? Send a webmention.

It shows up above once verified.


Previous Post
Certificate Expiry: Monitor Before the 3 AM Call
Next Post
Where Environment Variables Actually Live in Linux

Discussion

Powered by Garrul. Sign in with GitHub or Google, or post anonymously.

Related Posts