在 bash 中创建调用脚本和附加命令的别名

Creating an alias in bash that calls a script and an additional command

我正在考虑创建一个别名,允许我列出目标子目录的内容,而无需更改到该目录。

我已经成功地创建了别名和脚本来更改目录和显示内容。我将脚本称为 leap,它很简单:

#!/bin/bash

PATH=/bin:/usr/bin:.

# script to change to a directory, then display current contents
cd  && ls -l -a

我用来触发leap的别名定义为:alias lp='. ~/scripts/leap'

我希望我可以简单地创建一个名为 pk 的别名(pk 用于查看)并连接我的 leap 脚本和标准bash 命令使用 &&。我大错特错了。

当前目录

作为参考,这是我当前主目录的内容(我的用户 ID 已替换为 $$$$):

drwxr-xr-x 3 $$$$$$    46374   23 Aug 30 11:40 Fall_2019
drwxr-xr-x 5 $$$$$$    46374   66 Aug 28 09:01 PAST_COURSES
drwxr-xr-x 3 $$$$$$    46374   22 Aug 30 12:03 repos
drwxr-xr-x 3 $$$$$$ students  117 Aug 31 09:06 scripts

尝试

使用alias pk='lp && cd ..' 输入 [$$$$@host ~]$ pk PAST_COURSES 结果:

-rw-------    1 $$$$$$ students  1766 Feb 28  2018 ~
drwx------   10 $$$$$$ students  4096 Aug 31 09:06 .
drwx--x--x 1232 root   root     28672 Aug 30 16:03 ..
-rw-------    1 $$$$$$ students 11368 Aug 30 12:20 .bash_history
-rw-------    1 $$$$$$ students    18 Aug 21  2017 .bash_logout
-rw-------    1 $$$$$$ students   180 Mar  8  2018 .bash_profile
-rw-------    1 $$$$$$ students   526 Aug 30 11:19 .bashrc
-rw-------    1 $$$$$$ students   266 Aug 21  2017 .cshrc
drwxr-xr-x    3 $$$$$$     46374    23 Aug 30 11:40 Fall_2019
drwxr-xr-x    8 $$$$$$ students   155 Aug 30 12:14 .git
-rw-r--r--    1 $$$$$$ students    87 Apr 11  2018 .gitconfig
-rw-r--r--    1 $$$$$$ students 12288 Jan 29  2018 .hello.swp
-rw-------    1 $$$$$$ students   172 Aug 21  2017 .kshrc
-rw-------    1 $$$$$$ students   189 Mar 13  2018 .lesshst
-rw-r--r--    1 $$$$$$ students 20480 Jan 29  2018 .ls.swn
drwxr-xr-x    5 $$$$$$ 46374    66 Aug 28 09:01 PAST_COURSES
drwxr-----    3 $$$$$$ 46374    18 Aug 30 12:16 .pki
drwxr-xr-x    3 $$$$$$ 46374    22 Aug 30 12:03 repos
drwxr-xr-x    3 $$$$$$ students   117 Aug 31 09:06 scripts
drwx------    3 $$$$$$ students   103 Aug 30 11:12 .ssh
-rw-------    1 $$$$$$ students 12288 Sep  6  2017 .swp
drwxr-xr-x    2 $$$$$$ 46374    23 Aug 31 09:06 .vim
-rw-r--r--    1 $$$$$$ students  8129 Aug 31 09:06 .viminfo
-rw-r--r--    1 $$$$$$ students   142 Feb 14  2018 .vimrc
[$$$$@host home]$ 

如您所见,这显示当前目录 ( ~ ) 而不是切换到 PAST_COURSES 并显示它。此外,别名会跳到当前目录 ( ~ ) 之上的一个目录,而不是从 PAST_COURSES.

返回到它

顺便说一句,当我尝试使用以下别名时,我也得到了这个确切的结果:

pk='. ~/scripts/leap && cd ..' (使用 leap 的脚本而不是别名)

pk='cd && ls -l -a && cd ..' (使用 leap 中的确切代码)

调查结果

在我的修修补补中,我注意到了一些事情:

在这一点上,我需要一点帮助 - 不仅仅是脚本或别名来完成这项工作。解释我所看到的这种行为的解释会很棒。

不要使用 .(源内置)

alias pk='. ~/scripts/leap  && cd ..' (using the script for leap rather than the alias)

不等同于

alias pk='cd  && ls -l -a && cd ..'

在第一个中,使用了 . 内置函数(也称为 source),而在第二个中则没有。 . 不只是执行命令,它在 当前 shell 上下文 中执行。来自 documentation:

. (a period)

. filename [arguments]

Read and execute commands from the filename argument in the current shell context.

这意味着脚本所做的任何事情都会影响您当前的 shell 上下文。如果脚本更改目录,您当前的上下文也会更改。

另一方面,如果第一个版本像这样省略了 .

alias pk='~/scripts/leap  && cd ..'

然后 leap 脚本的内容会 运行 在它自己的 bash 上下文中,但是你当前的上下文会向上移动一个目录(因为 cd .. 是' 在 leap 脚本中)。

关于函数与别名的其他建议

您可以使用如下函数实现 pk

pk() {
  pushd 

  if [[ $? == 0 ]]; then
    # Successfully changed directories.

    # Run command
    

    # Return from the pre-pushd directory.
    popd
  fi

}

来自Bash Manual | 6.6 Aliases:

For almost every purpose, shell functions are preferred over aliases.

示例别名

alias foo="echo bar"

等效函数

foo() {
    echo bar
}