【Linux】一步一步学Linux——readonly命令(219)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/dengjin20104042056/article/details/100573595

00. 目录

01. 命令概述

readonly命令用于定义只读shell变量和shell函数。readonly命令的选项-p可以输出显示系统中所有定义的只读变量。

02. 命令格式

用法:
	readonly [-aAf] [name[=value] ...]
	readonly -p

03. 常用选项

-f: 定义shell函数
-a: 定义索引数组变量
-A: 定义关联数组变量
-p: 显示系统中全部只读变量和函数列表

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 ~]# 

4.2 定义只读变量并初始化

[root@localhost ~]# readonly AA="邓进"
[root@localhost ~]# echo $AA
邓进
[root@localhost ~]# AA="程序员"
-bash: AA: readonly variable
[root@localhost ~]# 

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 ~]# 

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

猜你喜欢

转载自blog.csdn.net/dengjin20104042056/article/details/100573595