00. 目录
01. 命令概述
readonly命令用于定义只读shell变量和shell函数。readonly命令的选项-p可以输出显示系统中所有定义的只读变量。
02. 命令格式
用法:
readonly [-aAf] [name[=value] ...]
readonly -p
- 1
- 2
- 3
03. 常用选项
-f: 定义shell函数
-a: 定义索引数组变量
-A: 定义关联数组变量
-p: 显示系统中全部只读变量和函数列表
- 1
- 2
- 3
- 4
04. 参考示例
4.1 显示只读变量
[root@localhost ~]# readonly
declare -r BASHOPTS="checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath"
declare -ir BASHPID
declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d"
declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")'
declare -ir EUID="0"
declare -ir PPID="31241"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="0"
[root@localhost ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
4.2 定义只读变量并初始化
[root@localhost ~]# readonly AA="邓进"
[root@localhost ~]# echo $AA
邓进
[root@localhost ~]# AA="程序员"
-bash: AA: readonly variable
[root@localhost ~]#
- 1
- 2
- 3
- 4
- 5
- 6
4.3 显示系统中全部只读变量和函数列表
[root@localhost ~]# readonly -p
declare -r AA="邓进"
declare -ir BASHPID
declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d"
declare -ir EUID="0"
declare -ir PPID="31241"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="0"
[root@localhost ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
评论记录:
回复评论: