1

Is there a way to actually execute results from a shell command, instead of using them as arguments to another command?

For instance, I'd like to run '--version' on all executables in a folder, something like:

ls /usr/bin/ | --version

I've found a way using find/exec:

find /usr/ -name valgrind -exec {} --version \;

But I'd like to do it with ls. I've search for over 45 minutes and can't find any help.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250

4 Answers4

2

Try doing this :

printf '%s\n' /usr/bin/* | while IFS= read -r cmd; do "$cmd" --version; done 
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
2

If you're trying to execute every executable file in a certain directory with a --version option, try this one-liner (using /usr/bin as the example directory):

for f in /usr/bin/*; do [ -x "$f" ] && $f --version; done
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
1

perl and bash;

ls -1 | perl -pe 's/\n/ --version;\n/g' | bash

or xargs;

ls -1 | xargs -I {} bash {} --version;
user1133275
  • 5,488
  • 1
  • 19
  • 37
0

to reevaluate shell output as shell input, use eval.

eval "$(your command here)"
mikeserv
  • 57,448
  • 9
  • 113
  • 229
  • 1
    We should note that `eval` must be used with care, at least sanity check the `eval` input first (And welcome back @mikeserv). – cuonglm Oct 24 '16 at 02:14
  • @cuonglm - just stopping by. I have less time even than before for this stuff lately. I've been homeless since my house burnt down last month – mikeserv Oct 24 '16 at 10:03
  • 2
    Ops, I'm sorry about that, hope you will be better soon. – cuonglm Oct 24 '16 at 11:50