brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: type -a lists everything a command name can mean in bash

Bash
~/tips/tip-shell-type-a-all-definitions

type -a echo

Shows every alias, builtin, function and file behind a command name.

When a command acts strangely, my first question is which one actually runs. which only knows about files. Bash has a fuller answer.

The type builtin with -a prints all the places that contain a command with that name. The bash manual says that includes aliases, reserved words, functions and builtins:

type -a echo

On a Linux machine with bash 5.2 the output is a shell builtin first and then two executables:

echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

The first line is the one bash uses. That is the quick way to see why a command behaves differently from what its man page says: the man page may describe the file, while your shell runs the builtin. Run it on a name like ls and you also find out right away whether an alias has replaced it.

Two related options from the manual:

  • type -t name prints one word, alias, keyword, function, builtin or file, which suits a script
  • type -ap name restricts the output to executable files found by searching $PATH

The caveat is the hash table. The manual says that with -p or -P, a name that is in the table of hashed commands prints the hashed value, which is not necessarily the file that appears first in $PATH. Combining -a with -p avoids that, because type then skips the hash table and does a plain PATH search.

The output wording belongs to bash, so do not parse it in a script. For a script that only needs to know whether a command exists, command -v name is documented in the same manual.

Further reading: bash man page (type builtin)

More Bash tips echo {01..10} cmd > file 2>&1 export HISTIGNORE='ls*:pwd:cd*' #rsync -av --delete /srv/media/ /mnt/backup/media/ APP_HOST=example.com All Bash tips (52) All topics

Link