Contents

Pipe to/from the clipboard in a Bash script

Introduction

The clipboard is a buffer that operating systems provide for short-term storage and transfer within and between application programs.

The clipboard provides an application programming interface by which programs can specify cut, copy and paste operations.

In the Post, I will introduct how to using xclip in Bash Script.

Installion

Linux

1
2
3
4
5
6
7
8
# You can install xclip using `apt-get`
apt-get install xclip

# or `pacman`
pacman -S xclip

# or `dnf`
dnf install xclip

Set-up

Bash

In ~/.bash_aliases, add:

1
2
alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"

Do not forget to load your new configuration using . ~/.bash_aliases or by restarting your profile.

Fish

In ~/.config/fish/config.fish, add:

1
2
abbr setclip "xclip -selection c"
abbr getclip "xclip -selection c -o"

Do not forget to restart your fish instance by restarting your terminal for changes to apply.

Usage

You can now use setclip and getclip, e.g:

1
2
3
$ echo foo | setclip
$ getclip
foo