Is there a good way to replace home directory with tilde in bash?
bash, path, regex
Solution
See this unix.stackexchange answer:
If you're using bash, then the `dirs` builtin has the desired behavior:
dirs +0
~/some/random/folder
That probably uses Bash's own C code that you pasted there. :)
And here's how you could use it:
dir=... # <- Use your own here.
# Switch to the given directory; Run "dirs" and save to variable.
# "cd" in a subshell does not affect the parent shell.
dir_with_tilde=$(cd "$dir" && dirs +0)
Note that this will only work with directory names that already exist.
Problem
Im trying to work with a path and replace the home directory with a tilde in bash, Im hoping to get it done with as little external programs as necessary. Is there a way to do it with just bash. I got ``` ${PWD/#$HOME/\~} ``` But thats not quite right. It needs to convert: ``` /home/alice to ~ /home/alice/ to ~/ /home/alice/herp to ~/herp /home/alicederp to /home/alicederp ``` As a note of interest, heres how the bash source does it when converting the \w value in the prompt: ``` /* Return a pretty pathname. If the first part of the pathname is the same as $HOME, then replace that with `~'. */ char * polite_directory_format (name) char *name; { char *home; int l; home = get_string_value ("HOME"); l = home ? strlen (home) : 0; if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/')) { strncpy (tdir + 1, name + l, sizeof(tdir) - 2); tdir[0] = '~'; tdir[sizeof(tdir) - 1] = '\0'; return (tdir); } else return (name); } ```