AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 678259
Accepted
Porcupine
Porcupine
Asked: 2021-11-20 05:23:48 +0800 CST2021-11-20 05:23:48 +0800 CST 2021-11-20 05:23:48 +0800 CST

bash脚本中的`type`命令,未显示所有路径

  • 772

当我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输出与终端的输出相同?

bash shell-script
  • 1 1 个回答
  • 128 Views

1 个回答

  • Voted
  1. Best Answer
    cas
    2021-11-20T05:37:24+08:002021-11-20T05:37:24+08:00

    看起来您已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用作它们的解释器并不是一个好主意。相反,在脚本本身中定义脚本所需的任何别名或函数,或从另一个文件中获取它们。或者只是将命令与脚本中需要的任何选项一起使用 - 别名可以方便地减少重复输入,这在脚本中并不是真正需要的。BTW,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.
    
    • 7

相关问题

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • MySQL Select with function IN () with bash array

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve