1

I have a plugin that generates and copies to the clipboard a ready-to-paste command. For now I open a new terminal, paste the command and execute it. I want to write a script that automatically opens a new terminal and runs that command using the clipboard as input.

I tried to use xclip but can't figure out how to pipe its output to gnome-terminal, and all of the threads I saw focus on the opposite problem: saving command output to clipboard which is NOT what I want to do.

What I tried: xclip -se c -o | gnome-terminal -e

It says "Failed to parse arguments: Missing argument for -e". So at the very least I have a syntax problem.

I have a feeling that I might not be approaching this problem the right way. Complete noob here, so the more details the better!

Thanks!

  • 1
    Did you give … | gnome-terminal -e - a try? – dessert Nov 2 '17 at 19:05
  • 1
    I thought this would be easy. The best I have gotten so far is gnome-terminal -e "$(xclip -sel c -o)" – J. Starnes Nov 2 '17 at 19:47
1

It seems like gnome-terminal can't take input from stdin, so you have to use command substitution:

gnome-terminal -e "$(xclip -se c -o)"

Also, if you want to perform word splitting and globbing on the clipboard contents before it's run, you can use this:

gnome-terminal -x $(xclip -se c -o)

If you need to hold the terminal open when the command completes, you could use this:

gnome-terminal -x bash -c "$(xclip -se c -o); read -p 'Press Enter to close.'"

Or if you want to go to an interactive prompt:

gnome-terminal -x bash -c "$(xclip -se c -o); bash"

For more ideas, see With a launcher for a terminal application, how can I keep the terminal open after the program is complete?

  • Note that gnome-terminal closes instantly once command you run exits, so one would have to spaw extra shell if one wants to keep gnome-terminal open and interactive – Sergiy Kolodyazhnyy Nov 2 '17 at 20:54
  • 1
    @SergiyKolodyazhnyy Oh yeah, I forgot I have mine set to stay open when the process exits. I would solve that with gnome-terminal -x bash -c "$(xclip -s c -o); read -p 'Press Enter to close.'" – wjandrea Nov 2 '17 at 21:15
  • OP, let me know if this works for you, since the details may change depending on what command you want to run, and how you want to run it. – wjandrea Nov 2 '17 at 21:28
  • Thanks @wjandrea, it works perfectly! For now I'm using gnome-terminal --tab-with-profile=PROFILENAME --working-directory="/PATH/" -x bash -c "$(xclip -s c -o); bash" to keep the command prompt active, I might also use your very convenient read -p 'Press Enter to close.' – MonkeyBack Nov 2 '17 at 21:36
  • Sorry I just saw that I can't edit after 5 minutes... So gnome-terminal --tab-with-profile=PROFILENAME --working-directory="/PATH/" -x bash -c "$(xclip -s c -o); bash" is fine for now, but I originally setup that specific profile to not close after command exits. However it displays a yellow header "The child process exited normally with status 0." that masks the first lines of output. I switched it back to closing after command exits and I'm using your ; read -p 'Press Enter to close.' which is more elegant and suited to my needs considering the command I'm running. – MonkeyBack Nov 2 '17 at 21:49

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.