xargs is the utility to use for converting stdin input to command-line arguments.
Naively, you could do the following:
ls | xargs ./myscript.sh
However, that will not work as expected with filenames that have embedded spaces, as they will be split into multiple arguments.
Note that ./myscript.sh $(ls) has the same problem.
If your xargs implementation supports the nonstandard -0 option for parsing NUL-separated input, you can fix this as follows:
printf '%s\0' * | xargs -0 ./myscript.sh
Otherwise, use the following, which, however, will only work if no filenames have embedded " characters (or embedded newlines):
printf '"%s" ' * | xargs ./myscript.sh
Since your stdin input comes from filenames in this case, you can use find, which essentially has xargs functionality built in:
find . -type f -maxdepth 1 -exec ./myscript.sh {} +
- Note that
find . -type f -maxdepth 1 in essence does the same as ls, but there are subtle differences, notably that each matching filename will be prefixed with ./ and that the filenames may not be sorted.
-exec ./myscript.sh {} + invokes your script with as many filenames ({}) as can fit on a single command line (+) - just like xargs does - which are typically all of them.
Note that both xargs and find ... -exec ... + could result in multiple invocations of the specified command, if not all arguments fit on a single command line.
However, given how long command lines are allowed to be on modern platforms, this will rarely happen - it only happens if you have a huge number of files in your directory and/or their names are really long.
{}button on the toolbar. Also please give the exact contents ofmyscript.shthat are involved with reading input and show, if possible, the output/results you get and what you wanted to get.somefrom the keyboard, rather than fromls?readjust reads from standard input, which in the pipeline shown is the output ofls, not the terminal.lsprogramatically is a very bad idea. See mywiki.wooledge.org/ParsingLsfor arg in $@is not the same asfor arg in "$@"(or simplyfor arg, as"$@"is the default target for aforloop), which would be what you wanted if you were passing arguments on the command line. (A pipe puts content on stdin, not the command line, which is a different matter). In that case, usage would be./myscript *to pass all files in the current directory.