bash 脚本中的“type”命令未显示所有路径

bash 脚本中的“type”命令未显示所有路径

当我echo "${PATH}" | tr -s ':' '\n' | nl从 bash 脚本内部和终端输入时,得到相同的结果:

     1  /home/nikhil/Documents/Git/Cs/Architecture/bin
     2  /home/nikhil/.local/bin
     3  /home/nikhil/opt/.nvm/versions/node/v16.13.0/bin
     4  /home/nikhil/opt/bin
     5  /usr/local/sbin
     6  /usr/local/bin
     7  /usr/sbin
     8  /usr/bin
     9  /sbin
    10  /bin
    11  /usr/games
    12  /usr/local/games
    13  /snap/bin
    14  /home/linuxbrew/.linuxbrew/bin
    15  /home/linuxbrew/.linuxbrew/sbin
    16  /home/nikhil/.cargo/bin
    17  /home/nikhil/.cabal/bin
    18  /home/nikhil/opt/go/bin
    19  /home/nikhil/.ruby/bin
    20  /home/linuxbrew/.linuxbrew/opt/fzf/bin

但是当我在 bash 脚本中和终端上输入以下内容时,我得到不同的结果:

# From Terminmal
$ type pandoc
pandoc is aliased to `/usr/bin/pandoc'
pandoc is /usr/bin/pandoc
pandoc is /home/linuxbrew/.linuxbrew/bin/pandoc
pandoc is /home/nikhil/.cabal/bin/pandoc
# From inside bash script
pandoc is /usr/bin/pandoc

为什么typebashscript 内部和终端的输出不同?如何使 bash 脚本type输出与终端输出相同?

答案1

看起来您已经type别名为type -a.从终端运行的任何 shell 脚本都不会继承别名,并且脚本默认以非交互模式运行。

由于脚本在非交互式 shell 中运行,因此~/.bashrc当 bash 运行脚本时不会获取脚本,因此不会加载在那里定义的别名。

没有-a,type将会“指示如果用作命令名称将如何解释它”- 即它将向您显示实际运行的内容。使用-a,它将向您显示所有可能的匹配项 - 可执行文件$PATH(直接和通过以下符号链接)、别名、函数)

例如在我的系统上,grep别名是:

$ type grep
grep is aliased to `grep --directories=skip --binary-files=without-match'

$ type -a grep
grep is aliased to `grep --directories=skip --binary-files=without-match'
grep is /bin/grep

$ type -P grep
/bin/grep

type如果我在 bash 的(非交互式)实例中运行,我的别名不会被继承:

$ bash -c 'type grep'
grep is /bin/grep

如果我强制 bash 以交互模式运行,它就会source ~/.bashrc(反过来,它会获取我的~/.bash-aliases文件)。

$ bash -i -c 'type grep'
grep is aliased to `grep --directories=skip --binary-files=without-match'

注意:仅仅让脚本用作解释器并不是一个好主意bash -i。相反,请在脚本本身中定义脚本中所需的任何别名或函数,或从另一个文件获取它们。或者只使用该命令以及脚本中需要的任何选项 - 别名可以方便地减少重复键入,而这在脚本中并不是真正需要的。顺便说一句,type-P选项通常是脚本中最有用的选项。

help type

type: type [-afptP] name [name ...]

Display information about command type.

For each NAME, indicate how it would be interpreted if used as a
command name.

Options:
  -a        display all locations containing an executable named NAME;
            includes aliases, builtins, and functions, if and only if
            the `-p` option is not also used

  -f        suppress shell function lookup

  -P        force a PATH search for each NAME, even if it is an alias,
            builtin, or function, and returns the name of the disk file
            that would be executed

  -p        returns either the name of the disk file that would be executed,
            or nothing if `type -t NAME` would not return `file`

  -t        output a single word which is one of `alias`, `keyword`,
            `function`, `builtin`, `file` or ``, if NAME is an alias,
            shell reserved word, shell function, shell builtin, disk file,
            or not found, respectively

Arguments:
  NAME      Command name to be interpreted.

Exit Status:
Returns success if all of the NAMEs are found; fails if any are not found.

相关内容