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.


$ pwd
/Library/Application Support/Google Earth/


$ 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.

  • 1
    I doubt it the copy is correct - what you want is the paste to deal with spaces - which app are you pasting in?
    – mmmmmm
    Commented May 30, 2012 at 18:17
  • Great question. You'll soon have rep enough to comment. Welcome to the site!
    – bmike
    Commented May 30, 2012 at 18:51
  • 1
    pwd | pbcopy works fine for me in Terminal. Finder/Edit/Show Clipboard confirms it's ok.
    – lhf
    Commented May 30, 2012 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
    Commented May 31, 2012 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
    Commented May 31, 2012 at 11:05

3 Answers 3


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.

Or, to print the pwd while also copying it to the clipboard, use tee and an output process substitution:

alias cwd='printf "%q\n" "$(pwd)" | tee >(pbcopy)'
  • 1
    does not work on mac
    – Macilias
    Commented Apr 14, 2016 at 7:47
  • 1
    Works on my Mac. Commented May 3, 2017 at 14:38
pwd | sed 's/ /\\ /g'

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

  • That will only escape spaces, which is not nearly enough, even though that is what the OP used as an example.
    – Old Pro
    Commented May 31, 2012 at 3:36
  • but this works in most cases the proper one from glenn didn't work out on my mac, there is no output at all
    – Macilias
    Commented Apr 14, 2016 at 7:58
  • 1
    You can also use it as alias when you declare a function: function escapePWD() { pwd | sed 's/ /\\ /g'; } and use this for alias: alias epwd='escapePWD'
    – Macilias
    Commented Apr 14, 2016 at 8:06

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
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
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)"'

You must log in to answer this question.

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