Linux shell programming study notes 33: type command

 Table of contents

  1. 0 Preface
  2. 1 Function and format of type command
    1. 1.1 Function of type command
    2. 1.2 Format of type command
  3. 2 Type command usage examples
    1. 2.1 Use the type command to view the shell’s built-in commands (take the echo command as an example)
    2. 2.2 Use the type command to view aliases (take the ls command as an example)
    3. 2.3 Use the type command to view shell built-in commands and aliases at the same time (take the echo and ls commands as examples)
    4. 2.4 Use the type command to view external commands (take the tty command as an example)
    5. 2.4 Use the type command to view internal commands, aliases and external commands (take echo, ls and tty commands as examples)
    6. 2.5 Use the type command to view functions
    7. 2.6 If we use built-in commands or aliases as self-

0 Preface

In DOS, the function of the type command is to view the contents of the file.

In Linux, the function of the type command is quite different from that in DOS.

1 Function and format of type command

We can use the help type command to view help information about the type command in bash, which includes the function and format of the command.

purpleEndurer  @ bash ~ $ 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.
typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
    Set variable values and attributes.
    
    Obsolete.  See `help declare'.
purpleEndurer  @ bash ~ $ 

 1.1 Function of type command

The type command can display information about the specified command and determine whether the given command is an internal command, an external command (file), an alias, a function, a reserved word, or does not exist (cannot be found).

1.2 Format of type command

type [-afptP] command 1 [command 2 ...]

Options

Options illustrate Remark
-a

Displays all locations of executable files that contain the specified command;

Includes aliases, built-ins, and functions if and only if the "-p" option is not used

all
-f Disable finding shell functions function
-p If the given command is an external instruction, its absolute path is displayed path
-P Forces a PATH search for every command given, even if it is an alias, built-in command, or function, and returns the name of the disk file that will be executed        
-t When the given command is an alias, shell reserved word, shell function, shell built-in command, external command (disk file) or is not found, "alias", "keyword", "function", "builtin", " file" or empty. type

2 Type command usage examples

2.1 Use the type command to view the shell’s built-in commands (take the echo command as an example)

purpleEndurer  @ bash ~ $ type            # 不接任何选项和参数,无显示
purpleEndurer  @ bash ~ $ type echo       # 接命令,显示命令类型
echo is a shell builtin
purpleEndurer  @ bash ~ $ type -t echo    # 对内部命令使用 -t 参数,会显示
                                          # builtin,表示其为内部命令
builtin
purpleEndurer  @ bash ~ $ type -p echo    # 对内部命令使用 -p 参数,无显示
purpleEndurer  @ bash ~ $ type -a echo    # 使用 -a 参数,会将PATH变量中
                                          # 包含echo的命令显示出来
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
purpleEndurer  @ bash ~ $ echo $PATH      # 查看PATH变量的值
/home/csdn/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
purpleEndurer  @ bash ~ $ 

 

2.2 Use the type command to view aliases (take the ls command as an example)

purpleEndurer  @ bash ~ $ type ls
ls is aliased to `ls --color=auto'
purpleEndurer  @ bash ~ $ type -t ls
alias
purpleEndurer  @ bash ~ $ type -p ls
purpleEndurer  @ bash ~ $ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
purpleEndurer  @ bash ~ $ 

 If we want to execute the actual command instead of the alias, we can use

Linux shell programming study notes 31: alias and unalias operation command aliasesicon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/134642886?spm=1001.2014.3001.5501

The method introduced in can also be judged by using the type command.

2.3 Use the type command to view shell built-in commands and aliases at the same time (take the echo and ls commands as examples)

purpleEndurer  @ bash ~ $ type echo ls
echo is a shell builtin
ls is aliased to `ls --color=auto'

purpleEndurer  @ bash ~ $ type -t echo ls
builtin
alias
purpleEndurer  @ bash ~ $ type -p echo ls
purpleEndurer  @ bash ~ $ type -a echo ls
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls

purpleEndurer  @ bash ~ $

 

2.4 Use the type command to view external commands (take the tty command as an example)

purpleEndurer  @ bash ~ $ type tty
tty is /usr/bin/tty
purpleEndurer  @ bash ~ $ type -p tty
/usr/bin/tty
purpleEndurer  @ bash ~ $ type -P tty
/usr/bin/tty
purpleEndurer  @ bash ~ $ type -t tty
file
purpleEndurer  @ bash ~ $ type -a tty
tty is /usr/bin/tty
tty is /bin/tty
purpleEndurer  @ bash ~ $ type -ap tty
/usr/bin/tty
/bin/tty
purpleEndurer  @ bash ~ $ type -apt tty
file
file
purpleEndurer  @ bash ~ $ 

2.4 Use the type command to view internal commands, aliases and external commands (take echo, ls and tty commands as examples)

purpleEndurer  @ bash ~ $ type echo ls tty
echo is a shell builtin
ls is aliased to `ls --color=auto'
tty is /usr/bin/tty
purpleEndurer  @ bash ~ $ type -apt echo ls tty
builtin
file
file
alias
file
file
file
file
purpleEndurer  @ bash ~ $ type -a echo ls tty
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
tty is /usr/bin/tty
tty is /bin/tty
purpleEndurer  @ bash ~ $ type -at echo ls tty
builtin
file
file
alias
file
file
file
file
purpleEndurer  @ bash ~ $ type -t echo ls tty
builtin
alias
file
purpleEndurer  @ bash ~ $ type -p echo ls tty
/usr/bin/tty
purpleEndurer  @ bash ~ $ 

2.5 Use the type command to view functions

Let’s first define a function:

function a()
{
  echo hello;
}

Then use the type command to view:

purpleEndurer @ bash ~ $ function a(){ echo hello; }
purpleEndurer @ bash ~ $ type a
a is a function
a () 

    echo hello
}
purpleEndurer @ bash ~ $ type -a a
a is a function
a () 

    echo hello
}
purpleEndurer @ bash ~ $ type -f a
bash: type: a: not found
purpleEndurer @ bash ~ $ type -t a
function
purpleEndurer @ bash ~ $ type -p a
purpleEndurer @ bash ~ $ type -P a
purpleEndurer @ bash ~ $  

It can be seen that the -p and -P options have no effect on the function.

2.6 If we use a built-in command or alias as a custom function name, how will the type command be displayed?

Let’s first define a function:

function ls()
{
  echo hello;
}

Then use the type command to view:

purpleEndurer @ bash ~ $ function ls(){ echo hello; }
purpleEndurer @ bash ~ $ ls
hello
purpleEndurer @ bash ~ $ type ls
ls is aliased to `ls --color=auto'
purpleEndurer @ bash ~ $ type -a ls 
ls is aliased to `ls --color=auto'
ls is a function
ls () 

    echo hello
}
ls is /usr/bin/ls
ls is /bin/ls
purpleEndurer @ bash ~ $ type -t ls 
alias
purpleEndurer @ bash ~ $ type -p ls 
purpleEndurer @ bash ~ $ 

Judging from the execution of the above command:

  • In terms of execution priority, functions take precedence over built-in commands.
  • Without any options, the type command does not process functions.
  • Use the -a option to process functions with the type command.

Guess you like

Origin blog.csdn.net/Purpleendurer/article/details/134804451