How to get the stdout of a command into the clipboard on Mac OS ?

ยท 258 words ยท 2 minute read

Mac OS Native Commandline Tools ๐Ÿ”—

Mac OS comes with pbcopy to copy text, and pbpaste to paste the previously copied text.

You can copy with pbcopy like this.

pbcopy < id_bus_ssh_pass.txt

or piping the standard output (stdout) into standard input (stdin) of pbcopy.

cat id_bus_ssh_pass.txt | pbcopy

You can use pbpaste to print out the previously copied text. pbpaste spits out the text into the standard output (stdout). So, you can use it in scripts like this.

pbpaste | sed 's/A/B/g' | cat > new-text-file.txt

In that command, pbpaste pastes the clipboard text into the stdout, then the sed program replaces every A character into B character, then the cat program concatenate the whole text output into a file called new-text-file.txt.

Fish shell Tools ๐Ÿ”—

If you installed FISH shell on your Mac OS, you can use the tool made by Fish team to manage system clipboard (fish_clipboard_copy). You can Pipe the output of a command into the input of it like this.

cat ~/id_bus_ssh_pass.txt | fish_clipboard_copy

Fish shell has a command line tool to paste from clipboard too. You can use it like this.

fish_clipboard_paste | sed 's/A/B/g' | cat > new-text-file.txt

That command has the same functions and meaning of that pbpaste command we discussed above.

I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , Facebook , Telegram and GitHub .