Search code examples
bashpipecopy-pastecd

How can I get cd | cbpaste to actually cd?


I surveyed SO and couldn't find this specific question. I've found this, but that is for Linux. I am on OSx 10.7.5. Furthermore, I have the exact problem using xclip, and I would prefer to use cbcopy/cbpaste because when I run those, X11 doesn't open. However, if someone manages to find one, then kudos to you!

I know how to pipe a command to copy using cbcopy. I know how to just paste. I assumed that I would be able to cd | cbpaste, but you know what happens when you assume...

Basically, I copy the pwd of one window:

$ pwd | cbcopy
/User/Name/dir/dir/dir/pwd

and then attempt to cd into it in another:

$ pwd
/User/Name
$ cd | cbpaste
/User/Name/dir/dir/dir/[pwd of the other window, because that's where I copied from]
$ pwd
/User/Name

And I haven't gone anywhere. It only spits out the clipboard.

The more I look at this, the more my cd | cbpaste makes no sense (the cd becomes invalid because I'm not actually telling it where to cd), but I figure there's gotta be a way to do what I'm trying to do and I just haven't found it on Google/SO.

I also have the same problem when I do this in the same window (i.e., I tested it by copying the pwd, cd to the root, and trying it again), which makes me think that my syntax is just wrong.


Solution

  • Piping over cbpaste to cd won't do anything except take you back to your home directory, since cd without any options does that. You need to put cbpaste inside an environment variable with $(cbpaste):

    $ pwd | cbcopy    [this works to copy the pwd]
    $ cd              [this takes you to the home directory]
    $ cd $(cbpaste)   [this takes you to the variable's 
                       value, which is the directory you copied]