1. Overview
Often, we need to copy the contents of an output from a command in the terminal. It can become tedious when we have to repeat this process using a mouse. Therefore, we can leverage the power of command-line utilities to make this process easier. In the tutorial, we’ll learn how to use the system clipboard using the command line. First, we’ll discuss how the clipboard works in Linux. Then, we’ll cover the utilities needed for both X11 and Wayland for copy-paste operations in the command line.
2. How the Clipboard Works in Linux
Clipboard is a process that makes it possible to share data between applications. The clipboard stores the data in a standard way in the system’s memory so that it behaves consistently across applications. Similarly, when we try to paste the clipboard data, the application requests the clipboard manager to provide it with the required data. However, we should know that the behavior of the clipboard might differ across different distributions. One key aspect of the clipboard management mechanism is the display server that is currently in use. For instance, the clipboard behaves differently under X11 and Wayland.
2.1. Clipboard in X11
On Linux, the clipboard works based on selections. A selection is a fundamental process to transfer data between applications. In X Window System, the two most common selections are the primary selection and the clipboard selection. Whenever we highlight text in an X11 application, the text is instantly stored in the primary selection buffer. Therefore, we can readily paste the data from the primary selection in another application – usually with a middle mouse button. On the other hand, clipboard selection is the classic way of copy-pasting data between applications, which is common in other operating systems. We explicitly copy the data and paste the data through the standard copy-paste operations.
2.2. Clipboard in Wayland
Wayland doesn’t have the concept of selection. However, Wayland manages the clipboard operations through an extension called wlr-data-control. It allows applications running on Wayland to interact with the display server to set retrieve clipboard data. Whenever an application copies the data to the clipboard, the display server decides whether to accept or deny the copied data. Once the data is accepted, it’s made available to the other Wayland applications. Therefore, it primarily focuses on security. In contrast, X11 doesn’t have a secure model for the clipboard operations.
3. Clipboard Piping in Bash Under X11
On X11, we have access to a wide range of CLI utilities that are suitable for clipboard operations. In this section, we’ll discuss the two most popular options.
3.1. xclip
xclip is a utility that copies and pastes to and from the X11 clipboard, primary selection, and other X selection. By default, it might not be installed on most Linux distributions. However, we can install it from the official package repositories with a package manager like apt:
$ sudo apt install xclip -y
Once installed, let’s verify it:
$ which xclip
/usr/bin/xclip
Now, when we need to copy the data to the clipboard, we can simply pipe stdout to xclip:
$ echo "Hello, World\!" | xclip -selection clipboard
The -selection specifies the type of X selection to use, which is clipboard in our case. Therefore, the copied data can be pasted with the simple paste operation (Ctrl+V). Moreover, there are two other selections that we can use as well: primary and secondary. Similarly, we can paste the data from the clipboard using the -o or -out option, which prints the data from the selection to the stdout:
$ xclip -selection clipboard -o
Hello, World!
Notably, we can use xclip for bidirectional clipboard operations. It can come in handy in scenarios like quickly copying the output of a command:
$ ps -aux | xclip -selection clipboard
Also, we can simply use a “here string” to find the data in the copied text:
$ grep ibus-dconf <<<$(xclip -selection clipboard -o)
baeldung 1425 0.0 0.1 318996 6912 ? Sl 22:04 0:00 /usr/libexec/ibus-dconf
This reduces the need for manually selecting the text inside the terminal. Moreover, we can make this process more convenient by creating aliases for copying and pasting, respectively:
$ echo "alias xc='xclip -selection clipboard'" >> ~/.bashrc
$ echo "alias xp='xclip -selection clipboard -o'" >> ~/.bashrc
Now, we can simply use the xp and xc aliases instead.
3.2. Alternative: xsel
xsel is another handy utility we can use as an alternative to xclip. Like xclip, we might need to install it on our machine first:
$ sudo apt install xsel -y
Once installed, we can simply copy the data to the clipboard:
$ xsel --clipboard <<<$(cat processes.txt)
The command copies the contents of processes.txt to the clipboard selection. In the same way, there are also –primary and –secondary selections that we can use. Similarly, we can paste the date using the –output switch:
$ xsel --clipboard --output
baeldung 1425 0.0 0.1 318996 6912 ? Sl 22:04 0:00 /usr/libexec/ibus-dconf
baeldung 2282 0.0 0.0 17736 2304 pts/0 S+ 22:27 0:00 grep --color=auto pipwire
4. Clipboard Piping in Bash Under Wayland
In Wayland, the clipboard handling is different from X11. The commands that are implemented specifically don’t work in Wayland directly. It’s because Wayland has implemented its own protocol for clipboard management. Fortunately, there are utilities that we can use to interact with the clipboard directly. One such utility is wl-clipboard. wl-clipboard is a utility that we can use to use for copy-paste operations on a Wayland desktop. By default, it might not be available on the Linux machine that uses Wayland. However, we can install it from the official repositories:
$ sudo apt install wl-clipboard -y
Once installed, it creates two binaries: wl-copy and wl-paste:
$ which wl-copy
/usr/bin/wl-copy
$ which wl-paste
/usr/bin/wl-paste
We can use these two commands to copy and paste data to and from the clipboard, respectively. So, let’s go ahead and copy the contents of a file to the clipboard:
$ cat process.txt | wl-copy
The contents of processes.txt are copied to the clipboard. We can now paste it using wl-paste:
$ wl-paste
baeldung 1425 0.0 0.1 318996 6912 ? Sl 22:04 0:00 /usr/libexec/ibus-dconf
baeldung 2282 0.0 0.0 17736 2304 pts/0 S+ 22:27 0:00 grep --color=auto pipwire
5. Conclusion
In this article, we learned how the clipboard mechanism works in Linux under X11 and Wayland. Then, we discussed the different utilities like xclip and wl-clipboard to carry out copy-paste operations inside the terminal.