我可以更改“cd”的位置吗“ 带我走?

我可以更改“cd”的位置吗“ 带我走?

具体来说,我想知道,在处理一个项目时,如果我这样做,cd它是否可以带我到项目的根目录,如果我在其他任何地方,那么我就会得到默认行为。

答案1

function projectcd() {
  arg="$1"
  if [[ ! -z $arg ]] ; then
    cd $arg
  elif [[ $(pwd) == /home/user/project/root/* ]] ; then
    cd /home/user/project/root/
  else
    cd
  fi
}
alias cd=projectcd

答案2

用于定义命令CDPATH的基目录cd

[ramesh@dev-db ~]# pwd
/home/ramesh

[ramesh@dev-db ~]# cd mail
-bash: cd: mail: No such file or directory
[Note: This is looking for mail directory under current directory]

[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
[Note: This is looking for mail under /etc and not under current directory]

[ramesh@dev-db /etc/mail]# pwd
/etc/mail

相关内容