Apple - Is it possible to have bash escape spaces in pwd?

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

pwd | sed 's/ /\\ /g'

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

Tags:

Macos

Bash

Iterm