Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I have a script that outputs a string that I would like to edit and then run as a command.

For example, I am using

cat ... | xsel -p

And then I want to edit the output of `xsel -op´ and run the edited string as a new command.

So I am trying:

cat ... | xsel -p
$(xsel -op) TAB

But this of course fails.

So how can I output the contents of xsel -op (in this case) to my command line, edit that new command, and then run the command?


share|improve this question
up vote 3 down vote accepted

From zshbuiltins:

-z Push the arguments onto the editing buffer stack, separated by spaces.

To output content of xsel to your command line:

print -z $(xsel -op)
share|improve this answer
Ok, better than print -s, works on zsh – simao Oct 16 '12 at 14:29

You can get it added to history (at least in bash, I'm not sure on zsh) by doing the following

history -s $(xsel -op)

Then retrieve and edit it.

I've since found that

print -s $(xsel -op)

works equivalently under zsh

share|improve this answer
this looks like what I want, but doesnt work in zsh – simao Oct 10 '12 at 10:16
I found you can do the same under zsh with print -s $(xsel -op) – Julian Oct 10 '12 at 10:25
yep it works. Thanks! – simao Oct 10 '12 at 10:41
you can even run fc immediately after history -s $(xsel -op) or print -s $(xsel -op) if you want to edit the command in $EDITOR (vi, nano, whatever). – cas Oct 11 '12 at 5:46

Rather than adding it to history manually, you can just press bash's shell-expand-line key (M-C-e):

   shell-expand-line (M-C-e)
          Expand the line as the shell does.  This performs alias and his‐
          tory expansion as well as all of the shell word expansions.  See
          HISTORY EXPANSION below for a description of history expansion.
share|improve this answer

I am not entirely sure what you are trying to do but something like this should work:

`xsel -op | sed 'do something'`

The backticks will cause BASH to execute, in this case, whatever is saved in X's primary selection buffer which you have just edited using sed. For example:

echo "find . -name foo" | xsel -p
`xsel -op | sed 's/foo/bar/`

The above commands will cause find to be run searching for files or folders named "bar".

share|improve this answer
Not really what I would like, I want to edit it in the terminal, not with sed – simao Oct 10 '12 at 10:17

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.