Create a simple and useful BASH clipboard
January 12th, 2010 | by Pyro222 |
Do you ever find yourself in a virtual terminal only to keep switching back and forth between windows for the same information over and over? Why not create a script that will display this information in your bash prompt when needed, and display nothing when not in need. While we’re at it, why not allow it to execute the contents of the clipboard as well. Read on for a simple solution.
The BASH script
Here is a simple bash script that when called upon will store the specified data into a text file which will be displayed in your prompt for easy recall. It can also be executed line by line by using the “-e” option.
Just paste the script below into a file called “bcb” then run “chmod 755 ./bcb”. Now you can copy the script somewhere like “/usr/bin” and have a custom and powerful bash clipboard at your fingertips.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/bin/bash o_fn="pathto/bashclip.dat" #clipboard data file #process command line case "$1" in -c) echo "clearing clipboard."; cat /dev/null > "$o_fn" ;; #clear data file -a) echo "appending $2 to clipboard."; echo "$2" >> "$o_fn" ;; #append to data file -n) echo "new clipboard, added $2".; echo "$2" > "$o_fn" ;; #fresh entry -e) echo "Executing clipboard contents $2"; while read LINE do $LINE done < $o_fn ;; #execute clipboard contents -l) echo "Listing contents"; cat $o_fn;; *) echo "usage: $0 [-c] [-e] [-a 'text'] [-n 'text]" ;; esac |
This script allows you to clear, append to, enter new, and execute the contents of the clipboard. You can even specify environment variables. For example (bcb -n “$PATH”) will store the contents of $PATH in the clipboard. Be sure to change the file location of “bashclip.dat” to a preferred location.
configure .bashrc
To have the clipboard display in your prompt, replace your “PS1=” entry in your “~/.bashrc” file with something like the following.
1 | PS1="\[\033[36m\]\$(cat pathto/bashclip.dat)\[\033[m\]\n\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" |
Thats it! Double check the location of bashclip.dat and you can now begin using your clipboard script.
Syntax
Here is a quick rundown of its use.
- To clear the clipboard type
- To clear and add a new entry type
- To Append to the clipboard type
- To execute the contents of the clipboard type
- To list the contents of the clipboard type
bcb -c
bcb -n “Text or $variable to add”
bcb -a “append text or $variable”
bcb -e
bcb -l



Sorry, comments for this entry are closed at this time.