How can I print a path in Terminal with the home folder path replaced by a ~?


But I want to something like this


1 Answer 1


Here's a function:

pwdr () {
  pwd | sed s,$HOME,~,

Add this function to a profile file and use pwdr.

In bash you can also use parameter expansion and avoid the call to sed by using the rather magically looking

echo ${PWD/~/\~}

The ${...} expression can be used anywhere a shell variable can be used as well, the echo is just for demonstration purposes.

  • Of course this may fail in corner cases, like if there is a directory called /Users/denz/foo/Users/denz which this would translate to ~/foo/~. Apr 4, 2015 at 18:19
  • @NateEldredge Made a small change to the sed way, the corner case should work ok now with both options.
    – nohillside
    Apr 5, 2015 at 6:01

You must log in to answer this question.

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