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 715159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:09:21+00:00 2026-05-14T05:09:21+00:00

xargs is widely used in shell scripting; it is usually easy to recast these

  • 0

xargs is widely used in shell scripting; it is usually easy to recast these uses in bash using while read -r; do ... done or while read -ar; do ... done loops.

When should xargs be preferred, and when should while-read loops be preferred?

  • 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-14T05:09:22+00:00Added an answer on May 14, 2026 at 5:09 am

    The thing with while loops is that they tend to process one item at a time, often when it’s unnecessary. This is where xargs has an advantage – it can batch up the arguments to allow one command to process lots of items.

    For example, a while loop:

    pax> echo '1
    2
    3 
    4
    5' | while read -r; do echo $REPLY; done
    1
    2
    3
    4
    5
    

    and the corresponding xargs:

    pax> echo '1
    2
    3 
    4
    5' | xargs echo
    1 2 3 4 5
    

    Here you can see that the lines are processed one-by-one with the while and altogether with the xargs. In other words, the former is equivalent to echo 1 ; echo 2 ; echo 3 ; echo 4 ; echo 5 while the latter is equivalent to echo 1 2 3 4 5 (five processes as opposed to one). This really makes a difference when processing thousands or tens of thousands of lines, since process creation takes time.

    It’s mostly advantageous when using commands that can accept multiple arguments since it reduces the number of individual processes started, making things much faster.

    When I’m processing small files or the commands to run on each item are complicated (where I’m too lazy to write a separate script to give to xargs), I will use the while variant.

    Where I’m interested in performance (large files), I will use xargs, even if I have to write a separate script.

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

Sidebar

Related Questions

Can someone please give me an example of using xargs on the below operation?
How can I make xargs execute the command exactly once for each line of
I'm trying to run the following command: find . -iname '.#*' -print0 | xargs
GNU xargs has option '-x'. The man page says: -x Exit if the size
sudo find /etc | xargs grep -i fedora > searchFedora gives: /etc/netplug.d/netplug: # At
I want to understand the use of xargs man in Rampion's code : screen
# find /home/shantanu -name 'my_stops*' | xargs ls -lt | head -2 The command
I'm trying to use xargs on a Cygwin Windows system to remove SVN files
I tried the following code unsuccessfully find Applications | xargs -0 grep Applications I
find ./dir -type f -iname *.t[argz]*[bz2] -print | xargs mv --target-directory=dir seems to fail

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.