Vamsi Krishna B
Vamsi Krishna B

Reputation: 11500

Copy shell script output to clipboard

Is there any easy way to implement the copy to clipboard option from the output of a shell script ?

Upvotes: 52

Views: 53091

Answers (6)

Markus Zeller
Markus Zeller

Reputation: 9153

If you do that on Windows 10 LXXS Ubuntu bash you can do following command, working also on WSL2.

Copy from WSL to Windows Clipboard

echo "What so ever..." | clip.exe

The other way around piping the Windows clipboard to WSL can be done with help of the PowerShell. It has a command called Get-Clipboard and a nice short alias gcb.

Paste inside WSL from Windows Clipboard

powershell.exe -Command gcb | cat

If you use this more often you could create an alias in the bash like

alias wcopy="powershell.exe -Command gcb"

and then quickly use as

wcopy | cat

Upvotes: 15

jeshio
jeshio

Reputation: 665

With WSL2 and sudo you can use this:

echo "What so ever..." | /mnt/c/Windows/System32/clip.exe

Upvotes: 1

Mau
Mau

Reputation: 1323

You can use pbcopy which is native for Mac OS.

Try this command:

echo "variable" | pbcopy

it will copy the string "variable" into your clipboard.

Upvotes: 48

user1103596
user1103596

Reputation:

echo prints a newline at the end as well. Incase anyone else hits the same issue, I used Mauro's approach but with the printf command so that it's just the string, no extra line:

For Mac:

printf "$YOUR_VAR" | pbcopy

Upvotes: 5

paxdiablo
paxdiablo

Reputation: 882716

That may depend on the environment you're using. With Gnome at least (I haven't tried the others but it may work), you can pipe your output as follows:

echo 123 | xclip
echo 123 | xclip -sel clip

The first goes to the mouse clipboard, the second to the "normal" clipboard.

Upvotes: 64

dogbane
dogbane

Reputation: 274878

You can use the xclip command.

 echo hello | xclip

Instructions for obtaining xclip are here.

Upvotes: 6

Related Questions