News for hackers. Stuff that matters.
Re:OS X Command Line Utilities by rjeli on Sunday August 30, 2015 @11:27AM (#10143548)
pbpaste and pbcopy never really occur in ambiguous situations, so I wrote the following shell script:

  function clip { [ -t 0 ] && pbpaste || pbcopy }
This is easier to type and allows for the following:

  % echo hello | clip
  % clip
  hello
Re:OS X Command Line Utilities by kbd on Sunday August 30, 2015 @05:47PM (#10144586)
> function clip { [ -t 0 ] && pbpaste || pbcopy }

That's great! Would you please explain why that works? Are there any cases you know of where that doesn't work as expected?

Edit: btw you need a semicolon after pbcopy to make that function valid on one line.

Re:OS X Command Line Utilities by godDLL on Sunday August 30, 2015 @05:54PM (#10144601)
I'm not the OP, but I'll give it a go.

`[` is a Bash built-in command also known as `test`. The `-t` parameter tells it to test if a file descriptor is open for `0`, which is also known as `STDIN`.

So if you're piping something into `STDIN` it will do `pbcopy`. If not -- it will do `pbpaste`.

Re:OS X Command Line Utilities by djeikyb on Sunday August 30, 2015 @05:57PM (#10144612)
from `man test`:

   -t FD  file descriptor FD is opened on a terminal
FD 0 is stdin I'd guess. Pretty clever!
Re:OS X Command Line Utilities by mhouse on Sunday August 30, 2015 @11:59PM (#10145362)
In light of the recent interest in fish shell, the equivalent in fish is:

  function clip
    if not tty >/dev/null
      pbcopy
    else
      pbpaste
    end
  end
Re:OS X Command Line Utilities by imahorse on Sunday August 30, 2015 @04:03PM (#10144328)
Wow, that's really useful. For everyone who is as bad as me in bash, to actually use it put it like this in your bashrc:

function clip() { [ -t 0 ] && pbpaste || pbcopy }

Re:OS X Command Line Utilities by JonahBraun on Sunday August 30, 2015 @07:32PM (#10144771)
I think you posted this because pasting the OP results in a syntax error. The problem is actually the list in the compound command needs to be terminated with a newline or semicolon. Both of the following are equivalent correct oneliners:

clip() { [ -t 0 ] && pbpaste || pbcopy;}

function clip { [ -t 0 ] && pbpaste || pbcopy;}