别名 - cd 后跟 ls

Rez*_*eza 5 linux alias cd

如何定义别名,以便当我定义别名时cd Abcd,其中“Abcd”是目录名称,该目录将更改为“Abcd”,然后显示ls该目录的内容?

Gar*_*ett 7

我相信您不能使用别名来完成此操作,但您可以定义一个函数来完成此操作:

#print contents after moving to given directory
cl()
{
    cd $@
    ls
}
Run Code Online (Sandbox Code Playgroud)

您可以将其粘贴到您的~/.bashrc文件中。

如果您希望覆盖内置cd命令,那么您可以这样做:

#print contents after moving to given directory
cd()
{
    builtin cd $@
    ls
}
Run Code Online (Sandbox Code Playgroud)