-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcbpaste
More file actions
executable file
·97 lines (88 loc) · 2.56 KB
/
cbpaste
File metadata and controls
executable file
·97 lines (88 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
#
# Requirements:
# - pbpaste (on macOS)
# - xclip (for X11 environments)
# - sed
#
# Author: James Cherti
# URL: https://github.com/jamescherti/bash-stdops
#
# Description:
# ------------
# The pbpaste command-line utility that reads the contents of the system
# clipboard and writes it to standard output (stdout). This allows users to
# access clipboard contents within scripts, the terminal, or other command-line
# tools.
#
# This script pastes content from the clipboard. It adapts based on the
# environment:
# - If running on macOS with pbpaste, it uses pbpaste.
# - If running in an X11 environment with xclip, it uses xclip to access the
# clipboard.
# - If no GUI environment is detected (no $DISPLAY variable), it reads from a
# file located at $HOME/.clipboard as a fallback clipboard.
#
# Usage:
# cbpaste
#
# License:
# --------
# Copyright (C) 2012-2026 James Cherti
#
# Distributed under terms of the GNU General Public License version 3.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
set -euf -o pipefail
paste_from_xclip() {
xclip -o -selection clipboard || return 1
}
paste_from_file() {
local cbfile="$HOME/.clipboard"
if [[ -f "$cbfile" ]]; then
cat "$HOME/.clipboard" || return 1
fi
}
remove_newline() {
if [[ "${CBPASTE_STRIP_NEWLINE:-}" -eq 0 ]]; then
cat
else
sed -z "s/\\n\+$//"
fi
}
main() {
# macOS
if type -P pbpaste &>/dev/null; then
exec pbpaste
fi
# Source bash-stdops library
local script_dir
script_dir="$(dirname "${BASH_SOURCE[0]}")"
# shellcheck disable=SC1091
source "$script_dir/.bash-stdops.inc.sh"
# Paste
local errno=0
if type -P xclip &>/dev/null && is_wayland; then
paste_from_xclip | remove_newline 2>/dev/null || errno=1
elif type -P wl-paste &>/dev/null && is_wayland; then
wl-paste --no-newline || errno=1
elif [[ "${DISPLAY:-}" = "" ]]; then
paste_from_file | remove_newline || errno=1
else
paste_from_xclip | remove_newline 2>/dev/null || errno=1
fi
exit "$errno"
}
main