I am trying this:
ls | sed 's/.*/"&"/' | xargs sh -- script.sh
for files:
but after this, script.sh executed only for:
-test 23.txt
xargs
, by default, assumes that the command it is expanding can take multiple arguments. In your example, xargs
would have executed
sh -- script.sh "-test 23.txt" "test24.txt" "te st.txt"
If your script only echoes its first argument, then you'll only see -test 23.txt
You can tell xargs
to execute the command for every input by using the -n1
flag.
In many cases, xargs
is not what you want, even coupled with the find
command (which has a useful -exec
action). When it is what you want, you usually want to use the -0
flag coupled with some flag on the other side of the pipe which delimits arguments with NUL
characters instead of spaces or newlines.