Bash中的可执行命令

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.0 bash处理完命令行,准备执行命令 明确地指定了路径 (如果是外部命令则记录到HASH)执行命令 存在同名函数 存在同名内置命令 HASH命中 存在于$PATH中 如果有command_not_found_handle执行之,如果没有则报错,并返回状态码127 yes no yes no yes no yes no yes no

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:" ;;
esac

set -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
  done
done

参考文档

  1. http://unix.stackexchange.com/questions/94775/list-all-commands-that-a-shell-knows
  2. https://linux.die.net/man/1/bash
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值