1

I am trying to write a Bash script that when activated (via a xbindkeys shortcut) will assign selected text to a variable. Something like this:

read selectedtext
echo "$selectedtext" | xclip

Obviously, the outcome will be an easy way to cut the current line to the system CLIPBOARD.

EDIT

I realize my original question is kind of confusing. Here's an example:

$ This is a line of text

Becomes:

selectedtext="This is a line of text"
echo "$selectedtext" | xclip
  • Your outcome is not very obvious as you are asking for two different things. At first you say you want to assign selected text to a variable, then you say you want to assign the "current line" to the clipboard. Are you trying to copy a variable or paste to a variable? – Jesse_b Jul 20 '17 at 16:34
  • Sorry for the confusion. See the edit above. – ddefranza Jul 20 '17 at 22:22
  • I'm still kind of confused by your question because of your "Becomes" section. It sounds like you want to copy some text to a variable but when you say echo "$selectedtext" | xclip you are copying a variable to the clipboard which I believe is the opposite of what you want. – Jesse_b Jul 20 '17 at 22:28
  • It's basically a workaround for the crazy way Linux handles clipboards. I'm hoping to create this variable so I can pass it to xclip and make it available globally. My terminal emulator, st, does not have the ability to pass info to the global clipboard (via text selection and keyboard shortcuts, not mouse highlighting) natively. – ddefranza Jul 21 '17 at 0:20
  • what, exactly, do you mean by "does not have the ability to pass info to the global clipboard"? Also, if "st" means "stterm" aka "suckless terminal" then it certainly supports the X primary selection by mouse, and you can copy to (Alt-Shift-c) or paste from (Alt-Shift-v or Alt-Shift-Insert) the clipboard. see man stterm. If you want something that supports ^X, ^V etc then use a different terminal emulator. – cas Jul 21 '17 at 2:55
1

Send clipboard to variable:

selectedtext=$(xclip -out -selection clipboard)
echo $selectedtext

Send variable to clipboard:

selectedtext="some values"
echo $selectedtext | xclip
  • Thanks but I'm not sure that will accomplish what I'm after. Specifically, I want to suck the selected text in a terminal (at the command line, not output) into a variable. – ddefranza Jul 20 '17 at 17:02
  • So my first example should do this. selectedtext=$(xclip -out -selection clipboard) . will set the variable "$selectedtext" to whatever string is currently in your clipboard. – Jesse_b Jul 20 '17 at 22:26

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.