type

Display information about command type.

Source: src/scripting/command_builtin.f90:25-86

Synopsis

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

Description

The type builtin indicates how each name would be interpreted if used as a command. It shows whether a name is a builtin, function, alias, or external command.

Options

OptionDescription
-aShow all matches (not just first)
-fSuppress function lookup
-pShow path only (for external commands)
-PForce PATH search even for builtins
-tPrint single word type

Output Types

TypeDescription
aliasShell alias
keywordShell reserved word
functionShell function
builtinShell builtin
fileExternal command

Usage

Basic Usage

type ls
# ls is aliased to 'ls --color=auto'

type cd
# cd is a shell builtin

type while
# while is a shell keyword

type git
# git is /usr/bin/git

Type Only

type -t ls
# alias

type -t cd
# builtin

type -t git
# file

Show All Matches

type -a echo
# echo is a shell builtin
# echo is /usr/bin/echo

Path Only

type -p git
# /usr/bin/git

type -p cd
# (no output - builtin)

Force Path Search

type -P echo
# /usr/bin/echo

Examples

Check Command Type

if [[ $(type -t git) == "file" ]]; then
    echo "git is installed"
fi

Find All Instances

type -a python
# python is /usr/bin/python
# python is /usr/local/bin/python

Script Validation

check_commands() {
    local cmd
    for cmd in "$@"; do
        if ! type -t "$cmd" &>/dev/null; then
            echo "Missing: $cmd" >&2
            return 1
        fi
    done
}

check_commands git make gcc

Alias Resolution

alias ll='ls -la'
type ll
# ll is aliased to 'ls -la'

type -t ll
# alias

Search Order

When resolving commands, fortsh searches in this order:

  1. Keywords (if, while, for, etc.)
  2. Functions
  3. Builtins
  4. Aliases
  5. PATH (external commands)

Use -f to skip function lookup.

Implementation

Source: command_builtin.f90:25-86

The function checks:

  1. is_shell_keyword() - Reserved words
  2. find_function() - User functions
  3. is_builtin() - Shell builtins
  4. find_alias() - Defined aliases
  5. find_in_path() - External commands

Exit Status

StatusCondition
0All names found
1Any name not found

Related Commands

CommandDescription
typeVerbose, shows all info
whichExternal command, PATH only
command -vPOSIX way to check commands

See Also

  • command - Run command, bypassing functions
  • which - Locate command
  • alias - Create aliases