How to copy and paste within a terminal in macOS or Linux?

Article autor
September 9, 2025
How to copy and paste within a terminal in macOS or Linux?
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

Sometimes we want to store some piece of information while using a terminal, for example, a result of an executed command. We usually save it into some temporary file which is going to be deleted after all. There’s a better way.

Copying and pasting on macOS

On macOS, there are two commands - pbcopy and pbpaste which leverage the system’s clipboard.

The pbcopy command puts input into the clipboard.

pbcopy “some text”

Piping output works surprisingly well:

ps aux | pbcopy

If you want to print the clipboard’s content all you need to do is run pbpaste.

Let’s say you want to copy and execute the file’s content. Of course, you may just use source file.sh but for the sake of this tutorial let’s assume that scenario.

First, create a file:

cat > file.sh <<EOF
echo "hello world"
EOF

Then you can copy its content with pbcopy:

pbcopy < file.sh

The last step is to execute the clipboard’s content:

pbpaste | zsh

Copying and pasting on Linux

You can create pbcopy and pbpaste commands on Linux. All you need to do is to add those two lines to the ~/.bashrc file and restart your terminal:

alias pbcopy=’xsel --clipboard --input’
alias pbpaste=’xsel --clipboard --output’

And… that’s it! I hope that those two commands will enhance your experience with the CLI.

Work with a team that keeps learning and building better software every day.

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

Multiple pattern-matching concatenations for the single string

Pattern-matching is one of the finest elixir-lang features. Whoever knows the power of this tool once, will want to use it forever.

Using Logger.info and Logger.debug in ExUnit tests

By default in the test env, Phoenix doesn't show Logger.debug/Logger.info outputs in the console.

How to convert string to camel and snake case in Elixir

Sooner or later you may need to convert a string in Elixir to a camel or snake case. With Macro module (available in Elixir without extra dependency) it's super easy.