11

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
tmp="something"
pbpaste > $tmp    # this doesn't get the ls command output :(
echo $tmp         # should not print something

2 Answers 2

15

Try

ls | pbcopy
tmp=$(pbpaste)

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

1
  • lol @patrix...thanks for that. extra points for humor.
    – KG -
    Mar 10, 2012 at 20:08
4

This works for me...

#!/bin/bash

ls -altr | pbcopy
tmp=`pbpaste`
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"!

1
  • Thanks @binarybob, this solution works too. yup backticks is important, also not to be mistaken for single quotes '
    – KG -
    Mar 10, 2012 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .