Paste clipboard content into a variable in bash using xclip
Asked Answered
S

4

8

I know this command will paste the clipboard contents into a file:

xclip -out -selection clipboard >> file.txt

If I want to paste clipboard content into a variable like a string what do I do?

Shophar answered 13/4, 2014 at 18:24 Comment(0)
L
6

To assign the output of a command to a variable, you can use command substitution:

myvar=$( command )
echo "$myvar"
Lyons answered 13/4, 2014 at 18:28 Comment(0)
Y
5

You can output by echo your clipboard contents using xclip:

clipboard_content=`xclip -o -selection clipboard`
echo "$clipboard_content"
Yod answered 9/1, 2021 at 18:23 Comment(0)
Y
0

You can output echo your clipboard by xsel also:

myvar=$( xsel -ob )
echo "$myvar"
Yod answered 9/1, 2021 at 18:26 Comment(0)
P
0

I am currently using

#!/bin/bash
# collect contents of clipboard
ln=$(xsel -ob)
#
# manipulate contents of the variable ln
#
# "post result back to clipboard, -n without newline at end
echo -n $ln | xclip -sel c

xsel and xclip were installed using Synaptic

Putsch answered 21/6, 2023 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.