Ubuntu: Run the output of another command
I have command line that outputs a different command line, for example:
> commandlineA param1 param2
commandlineB param1
I would like to have a single command line execution that will run commandlineB with the given parameter(s).
Is there any simple way to do that using a single command line or do I need to run a small bash file that will store the output in a variable and then run it?
Thanks
Solution 1:
You seem to have worked this out yourself, but you can use:
$(command parameter1 parameter2)
Edit of edit: the below is somewhat wrong, whilst it does run the command in a subshell, it turns out that the environment variables will still be available. Sorry for misleading people...
Edit: that will run in a subshell, any unexported environment variables won't be used. If you want to run a command in the same shell, you need to use:
eval $(command parameter1 parameter2)
Another answer given is to pipe through bash itself:
echo 'uname -a' | bash
That will also execute in a subshell, to run in the same shell you will need:
echo 'uname -a' | bash -c
Incidentally, you can also use back ticks instead of the $() syntax, but it's not recommended.
Solution 2:
Use this:
commandlineA param1 param2 | bash
Example:
echo "uname -a" | bash
Output:
Linux k1104 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux