优化bash使用体验:命令定制与功能扩展
1. 创建更便捷的cd命令
在日常使用中,我们经常需要进入深层目录,传统的cd命令在向上移动多级目录时,需要输入大量的../,操作十分繁琐。比如要向上移动四级目录,就得输入cd ../../../../。
为了解决这个问题,我们可以使用以下函数:
# cookbook filename: func_cd # Allow use of 'cd ...' to cd up 2 levels, 'cd ....' up 3, etc. (like 4NT/4DOS) # Usage: cd ..., etc. function cd { local option= length= count= cdpath= i= # Local scope and start clean # If we have a -L or -P sym link option, save then remove it if [ "$1" = "-P" -o "$1" = "-L" ]; then option="$1" shift fi # Are we using the special syntax? Make sure $1 isn't empty, then # match the first 3 characters of $1 to see if they are '...' then # make sure