1

How to execute an output string without bash's command substitution?

For example, if I type in terminal tshark, and the program isn't installed yet, I will recieve an error message like:

_some text_
sudo apt-get install tshark

Now I want to execute this last line without copy&paste or transcription.

If I try

$ tshark 2> >(tail -1)
sudo apt-get install tshark

I will get this last line I want to execute.

And if I want to execute this string, of course bash's command substitution works and install everything properly:

$ $(tshark 2> >(tail -1))

But if I don't use bash's command substitution, and pipe this to xargs like

$ tshark 2> >(tail -1) | xargs
sudo apt-get install tshark

I am wondering why piping to xargs seems to have no effect.

But if I try

$ tshark 2> >(tail -1) | xargs xargs

sudo prompt's me correct for password, and than apt-get abbort's after Y/N for installing or not.

The same result occurs if I pipe to bash.

Any response or hint is welcome.

  • 1
    xagrs treats stdin as a list of arguments: if you don't give a command on the RHS it's equivalent to xargs echo. AFAIK it has no way to know that the first argument should be treated specially (i.e. as the command). Something like xargs -I{} bash -c '{}' might work. – steeldriver Jul 13 '17 at 13:49
  • Ok, is there a way for executing the output with xargs? I have already tried xargs -I{} bash -c '{}'. This also abborts after Y/N: – John Goofy Jul 13 '17 at 14:08
1

Answer

You cannot reuse the output of a command that was not explicitly captured (pipes, etc.) unless you re-execute the command and capture the output yourself.

Bash does not store command output in any variables, unlike how it does store command exit codes and command history. Your terminal has the output and bash does not bother keeping a copy for itself as well.

So if you want to avoid command substitution all together, I am going to conclude you can't get there from here.

Options

If the real question is "How can I easily grab the output that I see on the line above in the terminal and slap it into the command prompt as a command without a lot of typing?" you can tackle this a couple of ways.

  1. Use readline shortcuts to make editing a command more painless.

For example, your response to @choroba's fine suggestion elsewhere is that editing the last command is too much typing. The readline shortcuts CTRL-a and CTRL-e can make editing the last command (reached with CTRL-p instead of the UP waaaay over to the right) much faster.

  1. Use a terminal multiplexer such as screen or tmux. I prefer tumx. Copying and pasting is pretty painless with a simple CTRL-a CTRL-[ to jump into copy mode and a few more VI keys and you have just copied the line you want right into the next prompt (with tmux setup with a CTRL-a leader key.

Hope this offers some other perspectives.

  • Yes thank you, I was looking for an faster way with less typing. The fastest way seems to be a combination with up and ctrl-a. – John Goofy Jul 13 '17 at 15:02
1

xargs executes the command you specify on the arguments read from standard input. If no command was specified, it uses echo.

With xargs xargs, the first xargs reads the commands and runs

xargs sudo apt-get install tshark

but there's no more stdin, so no more arguments are added. But as there's no stdin, there are also no replies to interactive questions - they are reading from stdin, which is connected to the pipe, now exhausted. You can try running apt-get with --yes. (Prompting for password works as passwords are not read from stdin for security reasons).

  • Thank you for the xargs's hints. But mostly I was asking "How to execute an output string without bash's command substitution?" Do you have any hints for that? – John Goofy Jul 13 '17 at 13:34
  • Why do you need to avoid command substitution? – choroba Jul 13 '17 at 13:48
  • I don't need to avoid command substitution. But it is more convenient to type after an output instead of using the arrow-keys for getting the last output and than going to the end of line to type a ) and than go to the beginning of the line to type $(. – John Goofy Jul 13 '17 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.