Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'd like to copy the current directory to the clipboard, something like: pwd | pbcopy. However, pwd does not escape the space, so something in "Application Support", for example, doesn't copy correctly. I don't seem to remember this always being the case, so I could have boffed something. Using iTerm2.

Is there bash setting to escape everything? having trouble searching, too many questions/topics about Spaces.app or esc.

Current:

$ pwd
/Library/Application Support/Google Earth/

Preferred:

$ pwd
/Library/Application\ Support/Google\ Earth/

Already seen posts: Copying the current directory's path to the clipboard and How to cd to a directory with a name containing spaces in bash?, which do not address this. I would have made a comment in the former, but I don't have the privileges.

share|improve this question
1  
I doubt it the copy is correct - what you want is the paste to deal with spaces - which app are you pasting in? –  Mark May 30 '12 at 18:17
    
Great question. You'll soon have rep enough to comment. Welcome to the site! –  bmike May 30 '12 at 18:51
1  
pwd | pbcopy works fine for me in Terminal. Finder/Edit/Show Clipboard confirms it's ok. –  lhf May 30 '12 at 23:58
    
@lhf Yes, should have checked in Terminal, which works as you stated. No luck in iTerm2, though. Maybe I should switch back... –  greenwar May 31 '12 at 6:52
    
How can this depend on the terminal emulator you're running? I've just tested and pwd | pbcopy works fine in iTerm2. –  lhf May 31 '12 at 11:05
show 4 more comments

3 Answers

up vote 6 down vote accepted

This command will escape spaces properly:

printf "%q\n" "$(pwd)" | pbcopy

You can alias it using something with history like cwd if you don't mind re-defining a different cwd

alias cwd='printf "%q\n" "$(pwd)" | pbcopy'

Ditch the pipe to pbcopy if you want it to work more like pwd and just print out the escaped path.

share|improve this answer
add comment

There is no built-in way to make pwd output escaped file paths, as this is generally not useful.

It does not make sense for pwd or pbcopy to be adding backslashes to what is copied. If you wanted to copy the path into a text file or web post, you would not want a backslash inserted into it.

Probably what you want to do is create as separate alias, like qwd, to print the quoted form of the current directory, or just escape the output of pbpaste, which is as easy as putting it in double-quotes:

bash-3.2$ pwd
/Users/user
bash-3.2$ cd test\ dir/untitled\ \"folder/
bash-3.2$ pwd
/Users/user/test dir/untitled "folder
bash-3.2$ pwd | pbcopy
bash-3.2$ echo "`pbpaste`"
/Users/user/test dir/untitled "folder
bash-3.2$ cd
bash-3.2$ pwd
/Users/user
bash-3.2$ cd `pbpaste`
bash: cd: /Users/user/test: No such file or directory
bash-3.2$ cd "`pbpaste`"
bash-3.2$ pwd
/Users/user/test dir/untitled "folder

Note that it is not just spaces that need escaping. Forward and backward slashes, star, question mark, ampersand, semi-colon, and other characters need escaping, too. Safest is just to use double-quotes as in the example, which will work even if the path includes double-quotes in it.

If you want to be perverse about it, you could make AppleScript quote the current directory for you:

bash-3.2$ alias qwd="osascript -e 'return quoted form of POSIX path of (POSIX file \"./\" as alias)'"
bash-3.2$ qwd
'/Users/user/test dir/untitled "folder'

Otherwise I mostly agree with Glenn, except, as above, I would alias the quoted form to qwd so as not to interfere with the normal pwd:

alias qwd='printf "%q\n" "$(pwd)"'
share|improve this answer
add comment
pwd | sed 's/ /\\ /g'

But I'm not sure this will ultimately fix your issue. pbcopy is copying exactly what it receives on stdin.

share|improve this answer
    
That will only escape spaces, which is not nearly enough, even though that is what the OP used as an example. –  Old Pro May 31 '12 at 3:36
add comment

Your Answer

 
discard

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.