I created a simple alias for xargs
, with the intend to pipe it when needed. It will simply run a command for each line of it. My question to you is, is this useful or are there better ways of doing this? This is just a little bit of brainstorming basically. Maybe I have a knot in my head.
# Pipe each line and execute a command. The "{}" will be replaced by the line.
# Example:
# find . -maxdepth 2 -type f -name 'M*' | foreach grep "USB" {}
alias foreach='xargs -d "\n" -I{}'
For commands that already operate on every line from stdin, this won’t be much useful. But in other cases, it might be. A more simplified usage example (and a useless one) would be:
find . -maxdepth 1 | foreach echo "File" {}
It’s important to use the {}
as a placeholder for the “current line” that is processed. What do you think about the usefulness? Have you any idea how to use it?
Yeah sorry then. It would be good to not use
ls
in your example though, someone who doesn’t know about that might read this discussion and think that’s reasonable.As for your original question, doing the
foreach
as a personal alias is fine. I wouldn’t use it in any script, since if anyone else reads that, they probably already know aboutxargs
. So using yourforeach
would be more confusing to any potential reader I think.I guess you are right. I even point such things out on others, so fair enough. I will update the example, as I don’t want someone to see this and take it as a good example. For the alias, I never use aliases in scripts anyway. These are always for interactive usage for me at least. In scripts i follow some other rules, such as use longer names for options (unless it is a really common one).