这些是用户定义的命令,它们是通过 shell 内置命令 alias 创建的,其中包含其它一些带有选项和参数的 shell 命令。这个意图主要是使用新颖、简短的名字来替代冗长的命令。
创建一个别名的语法像下面这样:
1 2
$ alias newcommand='command -options'
通过下面的命令,可以列举系统中的所有别名:
1 2 3 4 5 6 7 8 9 10
$ alias -p alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'
要在 Linux 中创建一个新的别名,仔细阅读下面的例子。
1 2 3 4
$ alias update='sudo apt update' $ alias upgrade='sudo apt dist-upgrade' $ alias -p | grep 'up'
$ type if then fi for while case esac else until if is a shell keyword then is a shell keyword fi is a shell keyword for is a shell keyword while is a shell keyword case is a shell keyword esac is a shell keyword else is a shell keyword until is a shell keyword
function function_name { command1 command2 ...... }
让我们看一看如何在一个名为 shell_functions.sh 的脚本中写 shell 函数。
1 2 3 4 5 6 7 8 9
#!/bin/bash #write a shell function to update and upgrade installed packages upgrade_system(){ sudo apt update; sudo apt dist-upgrade; } #execute function upgrade_system
这些是在 shell 中内置的 Linux 命令,所以你无法在文件系统中找到它们。这些命令包括 pwd、cd、bg、alias、history、type、source、read、exit 等。
你可以通过下面展示的 type 命令来列出或检查 Linux 内置命令:
1 2 3 4 5 6 7 8 9 10 11
$ type pwd pwd is a shell builtin $ type cd cd is a shell builtin $ type bg bg is a shell builtin $ type alias alias is a shell builtin $ type history history is a shell builtin