Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some text stored on my clipboard. I want to store this text onto a shell script "variable".

So something like:

ls -altr > pbcopy
pbpaste > $tmp    # this doesn't get the ls command output :(
echo $tmp         # should not print something
share|improve this question
up vote 5 down vote accepted


ls | pbcopy

And don't forget to remove the files called pbcopy and something you created with your first and second line.

share|improve this answer
lol @patrix...thanks for that. extra points for humor. – Kaushik Gopal Mar 10 '12 at 20:08

This works for me...


ls -altr | pbcopy
echo $tmp

You needed a pipe rather than redirect on the pbcopy line and you can use = to assign tmp to the result of pbpaste - remembering the backticks around pbpaste, of course, otherwise it will just echo the word "pbpaste"!

share|improve this answer
Thanks @binarybob, this solution works too. yup backticks is important, also not to be mistaken for single quotes ' – Kaushik Gopal Mar 10 '12 at 20:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.