Bash中的可执行命令

来源:互联网 发布:crossover linux 破解 编辑:程序博客网 时间:2024/06/05 16:43

I、可执行命令

Bash下的可执行命令分为四类:

1. Aliases

these are nicknames for a command with some options. They are defined in the shell’s initialization file (~/.bashrc for bash).

$ alias #查看所有定义的别名$ alias -p #查看定义别名的格式

2. Functions

they are snippets of shell code given a name. Like aliases, they are defined in the shell’s initialization file.

$ declare -f #查看所有定义的函数

3. Builtins

bash内置命令,不需要另外启动子进程来执行,所以所有的操作都会影响当前bash

$ help #查看所有的内置命令及其启用情况

4. External commands

they are independent of the shell,shell executes external programs by looking them up in the executable search path。

II、执行顺序

man bash #COMMAND EXECUTION
Created with Raphaël 2.1.0bash处理完命令行,准备执行命令明确地指定了路径(如果是外部命令则记录到HASH)执行命令存在同名函数存在同名内置命令HASH命中存在于$PATH中如果有command_not_found_handle执行之,如果没有则报错,并返回状态码127yesnoyesnoyesnoyesnoyesno

III、常用相关命令

# type (buildin)# Display information about command type, 查看一个命令属于哪一类$ type -t cmd# compgen (buildin) # Display possible completions depending on the options$ compgen -c # will list all the commands you could run.$ compgen -a # will list all the aliases you could run.$ compgen -b # will list all the built-ins you could run.$ compgen -k # will list all the keywords you could run.$ compgen -A function # will list all the functions you could run.$ compgen -A function -abck # will list all the above in one go.# 查看所有的外部命令case "$PATH" in  (*[!:]:) PATH="$PATH:" ;;esacset -f; IFS=:for dir in $PATH; do  set +f  [ -z "$dir" ] && dir="."  for file in "$dir"/*; do    if [ -x "$file" ] && ! [ -d "$file" ]; then      printf '%s = %s\n' "${file##*/}" "$file"    fi  donedone

参考文档

  1. http://unix.stackexchange.com/questions/94775/list-all-commands-that-a-shell-knows
  2. https://linux.die.net/man/1/bash
0 0
原创粉丝点击