-
-
Save juanpagfe/fac89f67594279241ac7672294cfc308 to your computer and use it in GitHub Desktop.
Bash script to implement copy to clipboard for macOS and Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # A command-line tool to simplify the copy of text generated in the terminal to | |
| # the clipboard | |
| # | |
| # Dependencies: | |
| # - MacOS: pbcopy | |
| # - Linux: xclip | |
| # | |
| # Installation: | |
| # Only move the directory to a bin directory depending on where you want to | |
| # have the script available. | |
| # | |
| # Go to the gist URL (https://gist.github.com/juanpagfe/fac89f67594279241ac7672294cfc308/edit) | |
| # and copy the raw url, then: | |
| # | |
| # curl -L https://gist.githubusercontent.com/juanpagfe/fac89f67594279241ac7672294cfc308/raw/902fcf5650489ce61b13c274f388c01ec58d9eb2/copy -o ~/.local/bin/copy | |
| # chmod a+x ~/.local/bin/copy | |
| # | |
| # Usage: | |
| # The script can receive a file as a positional argument or can read the | |
| # /dev/stdin (output that comes from a unix pipe) | |
| # | |
| # Usage with file: | |
| # copy /path/to/file | |
| # | |
| # Usage with pipe: | |
| # echo "Hello World!" | copy | |
| usage() { | |
| echo "Usage 1: $0 <file>" | |
| echo "Usage 2: <piped-command> | $0" | |
| } | |
| unameOut="$(uname -s)" | |
| case "${unameOut}" in | |
| Linux*) export MACHINE=Linux;; | |
| Darwin*) export MACHINE=Mac;; | |
| CYGWIN*) export MACHINE=Cygwin;; | |
| MINGW*) export MACHINE=MinGw;; | |
| *) export MACHINE="UNKNOWN:${unameOut}" | |
| esac | |
| value="" | |
| if [ -p /dev/stdin ]; then | |
| value=$(cat /dev/stdin) | |
| elif [ $# -gt 0 ]; then | |
| if [ -f "$1" ]; then | |
| value=$(cat "$1") | |
| else | |
| echo "Error: $file is not a valid file." | |
| usage | |
| exit 1 | |
| fi | |
| else | |
| echo "No input provided." | |
| usage | |
| exit 1 | |
| fi | |
| if [[ "$MACHINE" = "Mac" ]]; then | |
| echo "$value" | pbcopy | |
| elif [[ "$MACHINE" = "Linux" ]]; then | |
| echo "$value" | xclip -i -sel p -f | xclip -i -sel c | |
| else | |
| echo "System is not supported" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment