Bashスクリプトの 'type'コマンドがすべてのパスを表示しない

Bashスクリプトの 'type'コマンドがすべてのパスを表示しない

echo "${PATH}" | tr -s ':' '\n' | nlBashスクリプトの内部と端末で入力すると、同じ結果が表示されます。

     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。端末で実行されるすべてのシェルスクリプトはエイリアスを継承するわけではなく、スクリプトはデフォルトで非対話モードで実行されます。

スクリプトは非対話型シェルで実行されるため、~/.bashrcbashはスクリプトの実行時にスクリプトを選択しないため、そこに定義されているエイリアスはロードされません。

いいえ-atypeそうです。「コマンド名として使用する場合の解釈方法を示します。」- つまり、実際に実行中の内容が表示されます。使用-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

typebashの(非対話型)インスタンスで実行すると、エイリアスは継承されません。

$ 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.

おすすめ記事