|
|
declare 是一个内置的 Bash 命令,允许你在 shell 范围内更改变量的特征。
. d& B8 G3 O; V' S& o
9 c) {+ p1 L. F/ i$ H3 _它还可以对变量进行速写声明。最后,它使你可以访问变量。9 u5 Q0 r% ?: A+ E- _
! i: N5 p% j9 c7 `& r" t6 Ldeclare -A 创建一个 associative array 变量,一个键值对数组,其值由关键字索引。
8 B2 p' B1 x" `; g" b; ^: O/ n6 W; z9 n) @8 [# d3 Z* m4 R
除了影响其行为的变量之外,还可以为 Bash 函数赋予属性。2 L; J t/ I& `9 d; k. k! C( n
1 n5 u; U1 Z% B1 h9 ~/ F; b
declare 命令的语法使用 Bash( k- B. B X8 }6 p: R4 p
$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]
/ k# a3 n( I& {( ~1 ] [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] ...- V) H l* z! N g! G1 Y
选项作为 Bash 中 declare 命令的一般参数9 _% K% C; T/ Y
declare 内置命令接受以下选项作为通用参数:4 E, c+ M1 Y8 k0 _7 \
6 X9 Y J4 m6 }$ bash -c "help declare"- ` q( k0 T: u) r
输出:9 O% L: M4 f& K+ l, n* |
# E& G7 J$ H3 |& z5 L. k8 {declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]* M( E6 j$ P! `( o: V; H
Set variable values and attributes.
7 ?& s! [/ d2 j& d: c' H5 w Q Declare variables and give them attributes. If no NAMEs are given,
7 y! J; n$ `6 G2 i3 H5 m display the attributes and values of all variables.
2 t1 @4 E8 W4 v% p7 Q/ q Options:
; x5 d6 {7 J! M: T6 i6 y1 { -f restrict action or display to function names and definitions0 D& I% i4 M- s2 n/ k6 \4 I: [4 c
-F restrict display to function names only (plus line number and% b' p' {2 j- L+ Y" W4 O3 x
source file when debugging)1 D3 `% \' _ Y% d7 n
-g create global variables when used in a shell function; otherwise! C- A# g/ m- {
ignored8 p5 Z% n' Q [% ~) t" l; @3 D
-p display the attributes and value of each NAME
' i& B ]0 ^1 g6 E8 H8 ` l Options which set attributes:
! o. i$ F. t8 z H2 y' I) W/ | -a to make NAMEs indexed arrays (if supported)
7 ]7 R" ?: |# K1 _* H, p -A to make NAMEs associative arrays (if supported)$ _" m; f) o4 R
-i to make NAMEs have the `integer` attribute- D! \+ k# F$ J, V! R7 k6 R
-l to convert the value of each NAME to lower case on assignment
5 ?3 e6 S' m+ c. C -n make NAME a reference to the variable named by its value
) _ l3 X' ]- h' c8 [( U -r to make NAMEs `readonly`! T5 b/ S% h- B G9 U0 i* X
-t to make NAMEs have the `trace` attribute
2 N6 [0 b9 A0 S6 ?9 y: N( \/ `! G7 G -u to convert the value of each NAME to upper case on assignment/ J/ Y" j4 y1 b
-x to make NAMEs export
}8 [5 @9 n3 H Using `+` instead of `-` turns off the given attribute.( @5 i" [7 U& F" W
Variables with the integer attribute have arithmetic evaluation (see
4 i) [! {" e; j- Z4 X1 n1 ]+ I9 s the `let` command) performed when the variable is assigned a value.
: }- q [* C* h( J1 T9 r When used in a function, `declare` makes NAMEs local, as with the `local`
; U3 F* @$ w& Q command. The `-g` option suppresses this behavior.
2 x7 p2 E" U- N Exit Status:5 `' S3 ~1 c- ], v+ [4 G: a6 }
Returns success unless an invalid option is supplied or a variable' Y, L# T' j* v [9 W) ?9 H
assignment error occurs.* F4 \' @* }5 V& a- l" o
下面是一些 help 命令的示例,以查看它们在终端中的显示方式。值得注意的是,最后一个对于我们的 Windows Git Bash 用户来说是一个故障保护。5 e! s/ s1 I; z3 W, U" E
: i7 w$ {9 J0 ?
现在你已经阅读了 bash declare 的介绍和手册页,是时候查看一些使用 bash declare 的实际示例了。
7 u. |( }0 T: D
. A- D0 e3 k* [0 o+ Q# VBash 中 declare 命令中使用的整数类型
8 [- @& \8 w( O, }) F使用 -i 标志将变量声明为整数。你可以在下面的示例中看到变量 age 被指定为 integer 类型。
" {/ O6 n6 H7 M5 i- T9 |4 P. S$ D: \
当你分配不同类型的字符串值时,你可能会看到一条错误消息,上面写着 Not the correct type;而不是抛出错误,错误变量将设置为零。+ c9 P; s& U: s$ \
5 [% W1 q; }% y% @5 j0 _
$ declare -i num=7- F4 i) v2 c0 n) y
$ echo $num. T% J, C3 n# M8 q. A- z
输出:2 K/ w8 V* I4 Y2 j
# L, e4 [9 r. f$ V7
3 o7 f; ]+ t/ F+ [; o8 Q% \结果,将整数类型重新分配给字符串会产生 0。, S' J5 q8 K& q! }) S# s
6 s0 V6 }( o3 \& @$ R- P
$ num=string& P9 K( U2 H9 s- r
$ echo $num
/ N- e! g- l6 U* O/ {1 E2 F; v: U输出:6 ~9 p% y0 C: e, u* o% B" e9 |
/ t4 l( _" m5 e: U9 c
0
5 o: U2 M2 F5 D' _8 f" L* z F在 Bash 中声明只读变量, w) p7 P: X2 |" F: }
这个编码词描述了一个变量如何被赋予一个值并且不能被程序员或机器修改。它在整个程序的生命周期内保持不变。7 K1 B; K5 n* m
$ i8 ?' x7 X# \7 L5 o2 S- \( e1 \使用 -r 标志使你的变量常量为只读。6 p5 V4 N5 H' o0 ]3 o0 |2 A1 ~
# L- P8 K/ F: K* x$ L* _& E* J
$ declare -r num=7
# z9 t C+ n( @. Z3 M输出:% D+ R3 P# d4 b& i7 c
& Q1 O/ J( B: j" M; @bash: declare: num: readonly variable2 `, q' [8 [$ i& S
如你所见,输出结果显示 readonly 变量。) P& G L& O" |3 f5 @; Y
% q& k/ }" m/ a$ v8 K9 R8 T' a1 E
在 Bash 中分配变量时使用小写和大写转换
& Y3 G9 t) q) j; q将变量分配给 bash 时,可以使用 -l 小写和 -u 大写标志将其从小写更改为大写,反之亦然。
\+ F: R5 t$ |' `# t0 m
+ f; M& w. n6 i$ u) c1 ]* [$ declare -l name="THANOS"8 K$ W! [" N! A/ p* o
$ declare -u name1="thanos"
* W" {& t0 E- G' r; I$ echo $name $name1& G" o( @0 ?0 J
输出:+ d$ f' x& i {4 Y0 J) b" k
4 a; r u J2 _/ z; O$ fthanos THANOS0 I* o4 L1 L T) ^
在 Bash 中使用 Subshells 导出变量
6 C; K; I3 U2 P8 a( V如果你以前使用过 bash,你可能已经注意到人们使用 export 命令将声明的变量导出到脚本或 shell 会话中的子 shell。我们可以用 declare 命令做同样的事情。. [" b% |0 Y9 _8 F, k5 y6 N0 B+ u: K
; d, N [- c! g$ f8 Y+ ]-x 标志应该用于导出,而+x 标志将阻止属性被导出。8 \9 t8 X! r7 J; H8 y
6 C5 x- L' L* L. w& s4 w$ declare -x name=thor
4 c, p3 A0 g' y% c$ sh -c "echo $name"
; P y4 _; r; x1 `输出:
2 m5 F8 g$ E! p) `6 j: D* J( E% k, |: L7 |) _2 g3 c5 ]
thor
8 k# p( a# @3 ?$ Q" K1 |在 Bash 中检查是否指定了任何属性7 J3 ]0 v5 ` E# w9 Z+ `" I
使用 -p 标志,我们可以查看属性 variable 是否已定义。
4 D( M6 U% R: p, c& `* k: x; ]$ ^) M; {7 T' N* ?
$ a=5;b=6;c=7
1 o* S1 Z" ]; |- v$ declare -p a b c. u& g6 r; z2 }
输出:
' I) s% i! q( W8 P
1 y* B n: _9 T4 }0 ^declare -- a="5"
+ z9 [+ A9 W( N1 W8 }declare -- b="6"
2 J) G0 _* e9 c0 ^- }7 ?) g( x2 jdeclare -- c="7"9 ]' H: t( ?6 X0 N
指定关键字后,将有两个破折号 --。声明 -a 数组。声明 -n 数字。它用于显示变量的类型 nameref。
* a: ^- L) u" S p8 x5 k, i7 ^+ N( P+ A& ~6 k K
如果没有声明,它只会显示。
. @' `4 }& A ]! K8 b# ^" N4 H$ c0 s* S( z0 \! T2 S
检查 Bash 中的函数定义
( Z- u* N: e( }6 k-F 和 -f 标志可用于检查函数是否已声明和定义。我正在制作一个简单的 inevitable_thanos 功能。
p- W+ P" r% F. d3 Z
& F3 B+ P0 L1 K4 D) j6 y. o0 D$ function inevitable_thanos(){ echo "ironman"; }
( S$ \) K7 Y+ u$ declare -f
: P! s* U) N; _2 P+ n% @1 l输出:) i* G, |; x- I o* x7 P6 {
# g5 @$ U$ ?! W- f: \inevitable_thanos ()
1 T: q0 n! |% u G. C{
2 d5 I- G* e! R' Y echo "ironman"0 I, D4 {1 J0 L. g$ A
}" J" r R, y& F, \0 P
quote ()
( W m' \" F% t0 ?6 k) d1 {; y0 v9 B) e! q% w1 ~1 q! u
|
|