【Bash百宝箱】shell内建命令之pwd、readonly、return、shift
2016-09-20 21:03
573 查看
1、pwd
在shell中,内建(builtin)命令pwd,格式如下:pwd [-LP]
pwd命令用于打印当前工作目录的绝对路径,选项“-L”表示路径可能包含符号链接,选项“-P”表示路径为物理路径,不包含符号链接,不指定任何选项时,默认效果同选项“-L”。成功执行时,pwd命令的退出状态为0。
2、readonly
在shell中,内建(builtin)命令readonly,格式如下:readonly [-aAf] [-p] [name[=word] ...]
readonly命令用于声明变量或函数的属性为只读,声明后这些变量或函数不可修改。在声明只读属性时,“-a”用于索引即下标数组,“-A”用于关联即键值对数组,“-f”用于函数,不使用这三个选项时默认为变量,对函数没有影响,后面跟着变量名或函数名name,对于变量来说,可以在声明只读属性的同时进行赋值,函数则没有这个用法。不指定任何选项或者指定了选项“-p”时,显示当前的只读变量,包括数组,不包括函数。如果只使用选项”-a“或”-A“或”-f“,则相应的只显示当前只读的索引数组或关联数组或函数。成功执行时,readonly命令的退出状态为0。
如下是readonly命令的几个例子:
$ readonly declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath" declare -ir BASHPID declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d" declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="46" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")' declare -ir EUID="1000" declare -ir PPID="30101" declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor" declare -ir UID="1000" $ readonly foo=abc $ foo=123 bash: foo: readonly variable $ bar=abc $ readonly bar $ bar=123 bash: bar: readonly variable $ index_array=(apple pear banana) $ readonly -a index_array $ declare -A associate_array=([a]=apple [p]=pear [b]=banana) $ readonly -A associate_array $ index_array= bash: index_array: readonly variable $ associate_array= bash: associate_array: readonly variable $ foobar() { echo "it's a function";} $ foobar() { echo "it's a function";} $ readonly -f foobar $ foobar() { echo "it's a function";} bash: foobar: readonly function $ readonly -p declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath" declare -ir BASHPID declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d" declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="46" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")' declare -ir EUID="1000" declare -ir PPID="30101" declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor" declare -ir UID="1000" declare -Ar associate_array='([a]="apple" [b]="banana" [p]="pear" )' declare -r bar="abc" declare -r foo="abc" declare -ar index_array='([0]="apple" [1]="pear" [2]="banana")' $ readonly -a declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="46" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")' declare -ar index_array='([0]="apple" [1]="pear" [2]="banana")' $ readonly -A declare -Ar associate_array='([a]="apple" [b]="banana" [p]="pear" )' $ readonly -f foobar () { echo "it's a function" } declare -fr foobar
3、return
在shell中,内建(builtin)命令return,格式如下:return
return命令可以出现在两个地方。一个是函数中,用于退出函数,退出状态为n,不指定n时退出状态为函数中执行的最后一个命令的退出状态。另一个地方是shell脚本中用于退出脚本执行,且执行脚本时必须使用句点命令或source命令,退出状态为n,不指定n时退出状态为脚本中执行的最后一个命令的退出状态。如果在命令行执行return命令,就会出错:
$ returnbash: return: can only `return' from a function or sourced script
4、shift
在shell中,内建(builtin)命令shift,格式如下:shift
shift命令用于移动位置参数,向左移动n个位置,默认移动1个位置,移动时,左边的参数被覆该,右边的参数被弃用unset。n的最小值为0,最大值为位置参数的个数。n无效时,不进行任何移动,退出状态为1。
下面是shift命令在函数中应用的例子:
$ foo() { while shift; do echo ${1:-null}; done } $ foo a b c d b c d null $ foo() { while shift 2; do echo ${1:-null}; done } $ foo a b c d c null
相关文章推荐
- 【Bash百宝箱】shell内建命令之break与continue
- 【Bash百宝箱】shell内建命令之test
- 【Bash百宝箱】shell内建命令之eval、exec、exit、export
- 【Bash百宝箱】shell内建命令之bind、enable、ulimit
- 【Bash百宝箱】shell历史命令(history)
- 【Bash百宝箱】shell内建命令之hash
- 【Bash百宝箱】shell内建命令之help、type、let、logout
- 【Bash百宝箱】shell内建命令之alias与unalias
- 【Bash百宝箱】shell内建命令之冒号
- 【Bash百宝箱】shell内建命令之times、umask、unset
- 【Bash百宝箱】shell内建命令之declare、typeset、local
- 【Bash百宝箱】shell内建命令之echo、printf
- 【Bash百宝箱】shell内建命令之set、shopt
- 【Bash百宝箱】shell内建命令之builtin、command、caller
- 【Bash百宝箱】shell内建命令之trap
- 【Bash百宝箱】shell内建命令之cd、getopts
- 【Bash百宝箱】shell内建命令之句点与source
- 【Bash百宝箱】shell内建命令之echo、printf
- 【Bash百宝箱】shell命令
- 【Bash百宝箱】shell内建命令之read、readarray、mapfile