Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I type something in a Linux bash terminal with no X, and then use Ctrl+u, whatever I typed is stored in the bash "clipboard" (for lack of a better term), and I can type it again doing Ctrl+y.

How can I copy the results from a grep command on a text file to such bash clipboard? For example, if I have an INSTALL file like this:

./installprocedure --do-some-long-and-complicated-operation-on-dir dir1

How can I copy the content of a grep command so that it's available doing Ctrl+y? For example:

copy content to bash clipboard "grep installprocedure INSTALL"  
./installprocedure --do-some-long-and-complicated-operation-on-dir dir1 #cursor available here
share|improve this question may be related to this. – N.N. Jun 30 '11 at 14:30
On Mac OS X, you can pipe into pbcopy when you're in a GUI session (so not via SSH) and it puts it into the system-wide clipboard. pbpaste to retrieve. – Daniel Beck Jun 30 '11 at 14:47
added comment this is on linux bash terminal with no X – 719016 Jun 30 '11 at 14:48
It's actually "the kill ring in GNU Readline" if you want the correct name. – JdeBP Jul 1 '11 at 12:13
up vote 1 down vote accepted

As far as I know there is no direct way to feed the internal bash buffer, but if your aim is to have the string on the command line to be able to edit it, that can be done utilizing two bash features:

  1. Parameter substitution (as mentioned by RedGrittyBrick) and

  2. the readline function shell-expand-line:

    shell-expand-line (M-C-e)

    Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions.


$ $(grep installprocedure INSTALL)Meta+Ctrl+e

This will replace the command line with the result of the command substitution.

Expanding on that, you can create a readline macro to do that automatically on any arbitrary command, without the need to enclose in backticks or $():

$ bind '"\C-xs": "\C-a$(\C-e)\e\C-e"'

Pressing Ctrl+X followed by s will then move the cursor to the beginning of the line (\C-a), literally insert $(, move to the end of the line (\C-e), literally insert ), and finally call shell-expand-line (\e\C-e = Meta+Ctrl+e).

In other words, it will replace the command line with its output.

share|improve this answer

Are you sure you don't want command history editing? Or maybe command substitution?

Command history

In bash, press the up-arrow key to recall the previous command. You can edit the line in the usual way.

Alternatively you can refer to previous commands, and to parts of previous commands using the exclamation-mark notation.

Using the output of one command as parameters for another command

 ./bar barparam `grep foo foofile`

Using the output of one command as input for another command

  grep foo foofile | ./bar barparam

Using a shell variable as a named clipboard

  clipboard = `grep foo foofile`
  ./bar barparam $clipboard
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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