pbcopy & pbpaste for Linux

Bug #1854092 reported by psl
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
xsel (Ubuntu)
New
Undecided
Unassigned

Bug Description

This is not a bug report but request for enhancement. Ubuntu 18.04 doesn't have pbcopy & pbpaste commands.

Mac users can use two commands, pbcopy and pbpaste, to work with clipbopard in their scripts. No such "easy to use" solution is available at Linux! Could be pbcopy and pbpaste commands implemented at Linux? That will make migration of Mac user to Linux World easier... ;-)

There are several articles how to implement pbcopy and pbpaste at Linux, most of them use "alias", something like this:

```
# mimic osx pbcopy/pbpaste
alias pbcopy="xclip -selection clipboard -i"
alias pbpaste="xclip -selection clipboard -o"
```
or
```
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
```

I think that alias commands cannot be used in shell script, right? Is it possible to implement pbcopy and pbpaste in the way to be usable in scripts too? These commands could be included in "xclip" package.

BTW, pbcopy and pbpaste are binary executable files at OS X.

NOTE: Even archaic OS like Windows has CLI commands to work with clipboard data. DOS has command CLIP.EXE that writes data to clipboard and this command can be even used from scripts in WSL (Windows Subsystem for Linux). DOS has no command to read data from clipboard. PowerShell has full support for clipboard, it can read and write clipboard data with commands Get-Clipboard and Set-Clipboard (aliases can be used, gcb and scb).

psl (slansky)
description: updated
psl (slansky)
description: updated
Revision history for this message
psl (slansky) wrote :

This request for against xsel package was created by a mistake. I opened similar request for xclip package
https://bugs.launchpad.net/ubuntu/+source/xclip/+bug/1854113

description: updated
description: updated
Revision history for this message
psl (slansky) wrote :

I tried to write a simple script to collect data with "copy", script waits in the loop for data in clipboad and prints that text to stdout and clears clipboard. Simple and powerful! It works well with xclip but doesn't work with xsel, like some data are not cleaned and are put to the stdout again and again. This could be a bug in xsel. Maybe that I just miss something...

```
#!/bin/bash
# print data from clipboard to stdout, again and again...

MODE="xclip"
#MODE="xsel" # doesn't work well, clipboard is not cleaned and the same text is printed again and again :-(

function pbcopy()
{
   if [ "$MODE" == "xclip" ]; then
     xclip -selection clip -i
   else
     xsel --clipboard --input
   fi
}

function pbpaste()
{
   if [ "$MODE" == "xclip" ]; then
     xclip -selection clip -o
   else
     xsel --clipboard --output
   fi
}

# MAIN
echo -n "" | pbcopy # clear clipboard
while :; do
   TXT="$(pbpaste)"
   if [ -n "$TXT" ]; then
      echo "$TXT"
      echo -n "" | pbcopy # clear clipboard
   else
      sleep 0.1 # sleep 100ms
   fi
done

```

Revision history for this message
psl (slansky) wrote :

Modified version of previous demo. Clipbard is cleaned in different way.. The same result, xsel version doesn't work

```
# MAIN
pbcopy </dev/null # clear clipboard
while :; do
   TXT="$(pbpaste)"
   if [ -n "$TXT" ]; then
      echo "$TXT"
      pbcopy </dev/null # clear clipboard
   else
      sleep 0.1 # sleep 100ms
   fi
done
```

Revision history for this message
psl (slansky) wrote :

Do you know that even Windows have CLI command "clip" to send command input to clipboard? Try it:

```
C:\> DIR | CLIP
```

```
C:\> CLIP < README.TXT
```

Revision history for this message
psl (slansky) wrote :

Users of WSL (Windows Subsystem for Linux) can send data to clipboard in this way:

```
user@WIN10: $ ls -l | clip.exe
```

```
user@WIN10: $ file $(which clip.exe)
/mnt/c/Windows/System32/clip.exe: PE32+ executable (console) x86-64, for MS Windows
```

Revision history for this message
psl (slansky) wrote :

PowerShell scripts can read/write clipboard data too:

```
PS C:\Users\user> echo "Test 1234" | Set-Clipboard
PS C:\Users\user> Get-Clipboard
Test 1234
PS C:\Users\user> Set-Clipboard "Hello World!"
PS C:\Users\user> Get-Clipboard
Hello World!
```

psl (slansky)
description: updated
psl (slansky)
description: updated
Revision history for this message
psl (slansky) wrote :

PowerShell version of the script that waits for data in clipboard and send them to STDOUT

```
# scb is alias for Set-Clipboard
# gcb is alias for Get-Clipboard
# sleep is alias for Start-Sleep

# copy data from clipboard to stdout
# MAIN
Set-Clipboard # clear clipboard
while ($true) {
   $TXT = Get-Clipboard | Out-String # read clipboard
   if ($TXT) {
      Write-Host "$TXT" -NoNewLine
      Set-Clipboard # clear clipboard
   }
   else {
      Start-Sleep -Milliseconds 100 # sleep 100ms
   }
}
```

Revision history for this message
psl (slansky) wrote :

I am not strong in PowerShell. Previous script sends data from clipboard ONLY to console, it doesn't send them to STDOUT and such script cannot be used to pipe data to other commands. This is version of script that really sends data from clipbaord to the stdout:

```
# scb is alias for Set-Clipboard
# gcb is alias for Get-Clipboard
# sleep is alias for Start-Sleep

# copy data from clipboard to stdout
Set-Clipboard # clear clipboard
while ($true) {
   $TXT = Get-Clipboard # read clipboard
   if ($TXT) {
      Write-Output "$TXT"
      Set-Clipboard # clear clipboard
   }
   else {
      Start-Sleep -Milliseconds 100 # sleep 100ms
   }
}
```

Revision history for this message
psl (slansky) wrote :

A year ago I published in this bug report a script to copy data from clipboard to standard output. This is updated version, only small modifications to improve compatibility with dash shell.

```
#!/bin/sh
# print data from clipboard to stdout, again and again...

MODE="xclip"
#MODE="xsel" # doesn't work well, clipboard is not cleaned and the same text is printed again and again :-(

pbcopy ()
{
   if [ "$MODE" = "xclip" ]; then
     xclip -selection clip -i
   else
     xsel --clipboard --input
   fi
}

pbpaste ()
{
   if [ "$MODE" = "xclip" ]; then
     xclip -selection clip -o
   else
     xsel --clipboard --output
   fi
}

# MAIN
pbcopy </dev/null # clear clipboard
while :; do
   TXT="$(pbpaste)"
   if [ -n "$TXT" ]; then
      echo "$TXT"
      pbcopy </dev/null # clear clipboard
   else
      sleep 0.1 # sleep 100ms
   fi
done
```

Revision history for this message
psl (slansky) wrote :

Ubuntu 20.04 doesn't have pbcopy & pbcopy commands, issue is still ignored...

Revision history for this message
psl (slansky) wrote :

Another idea for pbcopy & pbpaste use, simple snippet processor that works in every application that works with clipboard. Could be useful if you have to fill forms with similar values again and again...

```
# MAIN
#pbcopy </dev/null # clear clipboard
OUT=""
while :; do
   TXT="$(pbpaste)" # read from clipbard
   if [ "$TXT" = "$OUT" ]; then
       # wait for a change
       sleep 0.1 # sleep 100ms
       continue
   fi
   OUT="$TXT"
   # define your macros/snippets
   case "$TXT" in
      "M1") OUT="MACRO1";;
      "M2") OUT="MACRO M2";;
      "IP1") OUT="fdf4:26f8:bebb:0:3973:b5eb:fe86:b000";;
      "IP2") OUT="fdf4:26f8:bebb:0:f1bf:a0f:efb:c2e2";;
      "IP3") OUT="2a00:1450:4014:80d::200e";;
      "!D") OUT="$(date '+%Y-%m-%d')";;
      "!T") OUT="$(date '+%H:%M:%S')";;
      "!!") OUT="$(date '+%Y-%m-%d %H:%M:%S')";;
      "!@") <email address hidden>";;
      # LOOP, Loop and loop, they cycle!
      "LOOP") OUT="Loop";;
      "Loop") OUT="loop";;
      "loop") OUT="LOOP";;
   esac
   if [ "$TXT" != "$OUT" ]; then
       echo "REPLACE: $TXT -> $OUT"
       echo -n "$OUT" | pbcopy # write to clipboard
   fi
done
```

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.