我试图用一个路径工作,并用bash替换一个波形符号的主目录,我希望用必要的少量外部程序完成它.有没有办法只用bash呢?我有
${PWD/#$HOME/\~}
但那不是很正确.它需要转换:
/home/alice to ~ /home/alice/ to ~/ /home/alice/herp to ~/herp /home/alicederp to /home/alicederp
作为一个感兴趣的注意事项,在转换\w value in the prompt时,应该怎么做bash源呢?
/* 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); }
见
this unix.stackexchange answer:
If you’re using bash,then the
dirs
builtin has the desired
behavior:06000
这可能会攻击您粘贴在那里的C代码. 原文链接:https://www.f2er.com/regex/357185.html