Linux命令:readonly

readonly [-aAf] [name[=value] ...] or readonly -p

 -A  表示后面的name变量都是关联数组

-a  表示后面的name变量都是index数组

-f  表示后面的name都是函数

不带任何name,或者-p,打印所有只读name,包括变量、函数。

-aA 同时出现,-A优先。

 如果-aAf 和名字不一致,会返回非0。

name后带value和不带value的区别是,带value表示在定义时就声明只读;不带value表示先定义后,后声明。对变量声明,既可以在定义变量时声明,也可以在变量定义后再声明。

对于函数,只能先定义后声明。

 取消只读????

help readonly

 1 readonly: readonly [-aAf] [name[=value] ...] or readonly -p
 2     Mark shell variables as unchangeable.
 3 
 4     Mark each NAME as read-only; the values of these NAMEs may not be
 5     changed by subsequent assignment.  If VALUE is supplied, assign VALUE
 6     before marking as read-only.
 7 
 8     Options:
 9       -a        refer to indexed array variables
10       -A        refer to associative array variables
11       -f        refer to shell functions
12       -p        display a list of all readonly variables and functions
13 
14     An argument of `--' disables further option processing.
15 
16     Exit Status:
17     Returns success unless an invalid option is given or NAME is invalid.

 例子

#  声明只读变量。
readonly var=value

# 声明只读数组
readonly -a array=(val1 val2 val3)
#  声明只读函数
readonly -f f1
 
本篇文章出自“国民时代”,转载请注明转载出处。
原文地址:https://www.cnblogs.com/ChinaGo/p/10626854.html