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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:18:31+00:00 2026-05-12T09:18:31+00:00

I have these files in a directory: y y1 y2 y3 Running this command:

  • 0

I have these files in a directory: y y1 y2 y3

Running this command:
ls y* | xargs -i basename {}|xargs -i sed "s/{}//g"

produces this:
1
2
3

Can someone explain why?! I expected it to produce nothing – running sed four times, once for each file, and removing the file name each time. But actually it looks like it’s applying sed with {} set to the first file, on a list of y1 y2 y3

This is Solaris 10

  • 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-12T09:18:32+00:00Added an answer on May 12, 2026 at 9:18 am

    When I try this on my linux box, I get inconsistent results. Sometimes 123, sometimes (most of the times) 23, sometimes 12. This is a subtle buffering race condition between the rightmost xargs and any of the sed it spawns.

    Dissecting the command line:

    • ls y* will output 4 lines, y, y1, y2 and y3; buffering not relevant
    • xargs -i basename {} will read them and launch, in a sequence, basename y, basename y1, basename y2, basename y3; output, same as input in our case, is line-buffered as each line comes from a different process.
    • xargs -i sed "s/{}//g", for each line X it reads (more on that later), launches sed "s/X//g"
    • each sed "s/X//g" filters out each X it sees in the lines it reads

    Where it gets tricky: the last two commands read input from the same stream. That stream is produced by multiple different processes in a sequence. Depending on a multitude of factors (system load, scheduling), the output could come out in very different timing patterns.

    Let’s suppose they’re all very fast. Then all four lines might be available for the right xargs to read in a single block. In that case, there would no input left for any of the seds to read, hence no output at all.

    On the other hand, if they were very slow, there might be only one line available for the right xargs on its first read attempt. That line would be “y”. xargs would spawn the first sed as sed "s/y//g", which would consume all remaining input (y1, y2, y3), strip y‘s, and output 1, 2, 3. Here’s the same explanation again, with more explicit sequencing.

    1. first basename writes “y”.
    2. right xargs reads “y”, spawns sed s/y//g. xargs now waits for sed to complete.
    3. second basename writes “y1”; sed reads “y1”, writes “1”
    4. third basename writes “y2”; sed reads “y2”, writes “2”
    5. fourth basename writes “y3”; sed reads “y3”, writes “3”
    6. left xargs is done; sed reads EOF and stops
    7. right xargs tries to continue, reads EOF and stops

    Not sure about my 12 case. Possibly GNU xargs doesn’t wait for its children to complete before it reads subsequent available input, and snatched the “y3” line from the first sed.

    In any case, you just set up a pipeline with multiple concurrent readers on the same writer, which yields mostly undeterministic results. To be avoided.

    If you wanted operation on each of the files, it would be avoided by specifying a filename to use by sed (note the final {}):

    ls y* | xargs -i basename {} | xargs -i sed "s/{}//g" {}
    

    If what you wanted was a cross-product-type result (strip each file name from each file), you’d need to arrange to have the file list produced as many times as there are files. Plus one for xargs, if you still used that.

    Hope this helps.

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

Sidebar

Related Questions

I have a python project with this directory structure and these files: /home/project_root |---__init__.py
Say I have this url: http://site.example/dir/ In this folder I have these files: test.ascx.cs
I have a website where users can upload their files; these are stored on
Hey, I have bash script running a wget command to get a directory: wget
I have a folder with these files: alongfilename1.txt <--- created first alongfilename3.txt <--- created
I have 4 files sorted alphabetically, A, B, C, and D. These files contain
I have a dozen configuration properties files for the ant script. These files are
I have an index.html and global.css files. When I open these files at Coda,
I have a few log files like these: /var/log/pureftpd.log /var/log/pureftpd.log-20100328 /var/log/pureftpd.log-20100322 Is it possible
I have a class split across two files. One of these is generated, the

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.