|
|
declare 是一个内置的 Bash 命令,允许你在 shell 范围内更改变量的特征。
* ]; c% i7 T' g/ D: t5 M! \+ S% \4 y
4 b& s! N5 Z0 ]: Y& g: h) y它还可以对变量进行速写声明。最后,它使你可以访问变量。7 |7 q0 z" s& s$ n* E
0 e8 f4 N* T4 J: Z7 A- N- @6 ?4 W; R% M& ]
declare -A 创建一个 associative array 变量,一个键值对数组,其值由关键字索引。
, t5 d% @5 X: P1 f7 n! e
2 d& D) A j6 C1 [: `除了影响其行为的变量之外,还可以为 Bash 函数赋予属性。
: h' I- S5 E6 j! Y; s( {2 \
* p& p: \3 r5 S8 L; Udeclare 命令的语法使用 Bash
/ R( Y# o! U$ ?% P) j* ~" w( A$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]
$ q3 d) d; ~( K/ i) I1 ?' P [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] ... }6 R* O9 l2 L3 ^( E
选项作为 Bash 中 declare 命令的一般参数
3 j' L! r/ {. d" x- {, Vdeclare 内置命令接受以下选项作为通用参数:
: W1 O% f, W/ [( N9 h' n) T& M, M, B3 N$ n
$ bash -c "help declare"/ m2 e3 P) S f u
输出:
( V, G' ?4 d, k6 ^8 s+ i) U: V" i6 B8 E2 A% ?" h
declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
& \( ^) [$ |* j4 S' n8 a Set variable values and attributes.$ [4 B! K& p+ u& s
Declare variables and give them attributes. If no NAMEs are given,
' B1 P, ]: r t; h6 u4 |" Z1 w display the attributes and values of all variables.
9 @ K. [9 U- E: }& Q" D d Options:
' Z- I0 T2 w6 W0 W7 p7 g- { -f restrict action or display to function names and definitions
" P' k V" G+ L! ` S/ H* I -F restrict display to function names only (plus line number and
& F: g4 g2 Z8 D' X1 l2 S+ } source file when debugging)
, u5 O0 H& q8 t -g create global variables when used in a shell function; otherwise
- u* F& Y0 i) z# B ignored4 i5 V5 s. x: p) L j
-p display the attributes and value of each NAME
6 u# }9 C# z" L& I. { Options which set attributes:! N+ F# d- _3 q. R
-a to make NAMEs indexed arrays (if supported)! R8 s8 w1 ], ?; h/ V0 S
-A to make NAMEs associative arrays (if supported)
& y6 v% e( Q7 n3 | -i to make NAMEs have the `integer` attribute
$ q5 E0 Z8 X- O* H- i, p -l to convert the value of each NAME to lower case on assignment; [! ^2 _& U. R% c4 W
-n make NAME a reference to the variable named by its value
, x! Y7 c/ ^3 B" t" c( F7 k; H -r to make NAMEs `readonly`0 X, h) f- m7 ~; Z8 l/ y$ h1 }0 L) w
-t to make NAMEs have the `trace` attribute9 r) u8 r+ I0 z; o2 m. |
-u to convert the value of each NAME to upper case on assignment, z3 Q9 u9 g: ]% {4 V/ h8 G
-x to make NAMEs export) i8 q( m4 E; U( u) ]
Using `+` instead of `-` turns off the given attribute.6 ~6 }5 ?- v: Q- n: J
Variables with the integer attribute have arithmetic evaluation (see
/ I) t3 _2 K1 c) J the `let` command) performed when the variable is assigned a value.1 E' E0 k6 H% J" L: r% c
When used in a function, `declare` makes NAMEs local, as with the `local`
, G/ u5 b' [. [$ b; g command. The `-g` option suppresses this behavior. Y5 R: _3 K' `9 r+ i* p- M
Exit Status:0 i6 d# y% R% q. a9 L* f
Returns success unless an invalid option is supplied or a variable
4 _ {0 ~' t7 p assignment error occurs.
% O7 Y% G# i. J下面是一些 help 命令的示例,以查看它们在终端中的显示方式。值得注意的是,最后一个对于我们的 Windows Git Bash 用户来说是一个故障保护。
1 J* Y R4 i) i# m o( W2 J9 n H8 U$ X
现在你已经阅读了 bash declare 的介绍和手册页,是时候查看一些使用 bash declare 的实际示例了。3 L" U- R4 X2 _- K2 x6 l! w7 {
. ~6 H R$ S1 K H1 _8 w6 R
Bash 中 declare 命令中使用的整数类型 a7 ]* m/ L+ d5 D+ J& D
使用 -i 标志将变量声明为整数。你可以在下面的示例中看到变量 age 被指定为 integer 类型。
$ K5 }8 W9 f" S N
0 _9 p2 e' I2 u, w/ I+ W# C q( x当你分配不同类型的字符串值时,你可能会看到一条错误消息,上面写着 Not the correct type;而不是抛出错误,错误变量将设置为零。
. |6 \" ?# Y" E u3 D
+ o; a: j: d+ K& ]0 N1 V+ V$ declare -i num=7
$ D* h( @: h9 o7 n0 B8 a( q$ echo $num; p& N1 D7 j- \$ o8 N0 q* q
输出:" {# A& E8 p1 ~* \7 f, X
, l1 ^( q+ q0 J5 X" X5 {74 L" X; s1 v n1 g- W: |, G8 d
结果,将整数类型重新分配给字符串会产生 0。% k8 ?- ]3 t U/ u% |. T
3 K% O4 B1 ]7 T8 u$ num=string3 B0 \4 k O7 @7 a8 X
$ echo $num( @7 T4 L7 K& K! S
输出:$ }; a) h) O; b7 u( L V
( ]% J" G% N$ z# h/ ^& X0 v0! e$ V2 H; I* Q2 x# {
在 Bash 中声明只读变量+ {; {7 O' W/ |! `" _: o% \
这个编码词描述了一个变量如何被赋予一个值并且不能被程序员或机器修改。它在整个程序的生命周期内保持不变。$ `( C* j5 ~$ ~& g. Z2 s1 k
8 J7 d) t! ~$ W
使用 -r 标志使你的变量常量为只读。/ @9 _$ k* p, W1 P% C! W
p5 e& V$ a4 e' `$ declare -r num=7" ?* B, z) L. t' {/ f5 ` |2 Y
输出:6 t/ l3 S4 G! X
- a6 s. v; ?# L! xbash: declare: num: readonly variable' s7 `2 g6 c+ x
如你所见,输出结果显示 readonly 变量。3 _8 G. D* {7 m/ d1 _0 E/ u
" \& E) _3 d7 e, i! `
在 Bash 中分配变量时使用小写和大写转换
0 i. c3 U& X7 u/ a' ?: \/ I将变量分配给 bash 时,可以使用 -l 小写和 -u 大写标志将其从小写更改为大写,反之亦然。
3 S0 q" R2 p) K* m5 {% v6 K: P
6 P$ e+ d8 P, J4 r, A* G$ declare -l name="THANOS"
/ \. a! T% V- i1 G! `* R4 a' O) P$ declare -u name1="thanos"& n/ W$ x5 P: q% M' \+ T3 L9 i
$ echo $name $name1
; k. R, w# p7 Y# g- h7 P输出:
i% B. Q; _" }5 V$ D+ T6 y
. T' R3 J2 l+ ]thanos THANOS
; ^0 P( L% a4 ~0 @0 \在 Bash 中使用 Subshells 导出变量; ?- i# _/ @1 c2 T0 O- f
如果你以前使用过 bash,你可能已经注意到人们使用 export 命令将声明的变量导出到脚本或 shell 会话中的子 shell。我们可以用 declare 命令做同样的事情。
5 j M& R2 w* N
2 Z+ v) D9 j4 B* N6 G$ o-x 标志应该用于导出,而+x 标志将阻止属性被导出。
7 I3 r7 `+ I8 j) b+ T, J, x \8 j" D/ m6 j: P
$ declare -x name=thor
5 R4 P- v, h. I9 C' O$ sh -c "echo $name"! Q" `$ v7 L+ Z) J
输出:
( H* o9 p/ B1 Q" u0 {) l
9 q; r' H3 j' jthor6 C4 g* Q" O/ |0 m2 o, O2 A8 ~
在 Bash 中检查是否指定了任何属性
5 P- M& [7 d2 q; x使用 -p 标志,我们可以查看属性 variable 是否已定义。$ I+ o/ V( U! G) e' T
0 W! }7 c' {( Z
$ a=5;b=6;c=7
) a1 w" w$ }; Q4 H" N$ declare -p a b c
0 a9 f( |- Q' w4 ^1 H4 p9 U/ B输出:
! d$ u; U# n) F
, S% e3 c; u2 ]2 a$ |declare -- a="5"& U% ^3 I( `4 }( x
declare -- b="6"* ~# `& A0 o! N9 ^) g3 h0 C
declare -- c="7"
7 D" t" B+ t5 v4 b- J0 X& i指定关键字后,将有两个破折号 --。声明 -a 数组。声明 -n 数字。它用于显示变量的类型 nameref。
5 H3 r) m1 w0 J
6 |* g9 A7 o3 a5 q* F9 K如果没有声明,它只会显示。
( M- u/ r* S5 Z2 s7 e* T# \% P
$ u7 I/ G) P8 t8 V3 x" \检查 Bash 中的函数定义
& o. }2 @. D$ N-F 和 -f 标志可用于检查函数是否已声明和定义。我正在制作一个简单的 inevitable_thanos 功能。7 l% ~, [3 ~. a5 J: }
0 G" F5 E8 f& s" V
$ function inevitable_thanos(){ echo "ironman"; }9 u" n* c$ N; [& g# |
$ declare -f
8 q f0 {% q& X输出:+ X- ?' m$ l( F: c$ A
( I% I- I+ B: z! @: \% f
inevitable_thanos (). }0 p- I, {( `& y1 t
{2 g$ v( d% y' b* S+ i
echo "ironman"" x. d' C& A; f3 j, G" P; k6 j8 {4 n
}
6 P. ]& }. u( {2 c2 kquote ()
3 @; e7 M9 w$ h# v) B
( D3 p. ^" ^; n# F$ _) w8 q: m6 S |
|