It looks like the open command can't take data from the standard input, so I ended up using something like this.
some_command | xargs -r-I{} open {}
For those who don't know, xargs (with the right arguments) can act like the map method on javascript arrays. So you can imagine the above doing something like this:
Recovering interrupter with occasional relapses, lover of spreadsheets, blogger, programmer, adept debugger, conjurer of analogies, and probably other things.
xargs can come in handy in a pinch, but it's usually unnecessary and the way it generates arguments can cause some complications. I can't think of a case where xargs would be more appropriate than Command Substitution or a while read loop, e.g.:
open "$(some_commands)"#thus you fully control word-splitting
or
some_commands | \while IFS=read-r line;do#unsetting IFS here prevents word-splitting
some_other_commands "$line"
even_some_more_commands "$line"#vars declared in here are not valid afterwards,#since they are declared inside the pipeline subshell#and its environment does not hoist#This includes $linedone#alternatively, using process substitution:while IFS=read-r line;do
some_other_commands "$line"#variables set here remain valid afterwards#since this while-loop is not in a subshell#thus the last $line will remain set afterwardsdone < <(some_commands)
With process substitution and the mapfile builtin, you can even populate an array and iterate over that:
I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch.
I used xargs this week.
It looks like the
open
command can't take data from the standard input, so I ended up using something like this.For those who don't know,
xargs
(with the right arguments) can act like themap
method on javascript arrays. So you can imagine the above doing something like this:xargs
is some great shell magic. And your JS translation helps me better think about it.xargs
can come in handy in a pinch, but it's usually unnecessary and the way it generates arguments can cause some complications. I can't think of a case wherexargs
would be more appropriate than Command Substitution or awhile read
loop, e.g.:or
With process substitution and the
mapfile
builtin, you can even populate an array and iterate over that:Nice!