使用xclip将剪贴板内容粘贴到bash中的变量中

时间:2014-04-13 18:24:34

标签: bash clipboard xclip

我知道这个命令会将剪贴板内容粘贴到文件中:

xclip -out -selection clipboard >> file.txt

如果我想将剪贴板内容粘贴到像字符串这样的变量中,我该怎么做?

3 个答案:

答案 0 :(得分:4)

要将命令的输出分配给变量,可以使用命令替换:

myvar=$( command )
echo "$myvar"

答案 1 :(得分:2)

您可以使用 from pynput.keyboard import Listener, Key, KeyCode #allowed = [KeyCode.from_char('7'), KeyCode.from_char('9')] #allowed = [KeyCode.from_char(char) for char in ['7','9','1','3','4','5','6','q','w','e','z']] allowed = [KeyCode.from_char(char) for char in '7913456qwez'] def on_press(key): try: print('allowed:', key in allowed) except Exception as ex: print('Error:', ex) def on_release(key): #print('{0} released'.format(key)) if key == Key.esc: # Stop listener return False with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() 通过 echo 输出剪贴板内容:

xclip

答案 2 :(得分:0)

您也可以通过 xsel 输出 echo 您的剪贴板:

myvar=$( xsel -ob )
echo "$myvar"