|
|
declare 是一个内置的 Bash 命令,允许你在 shell 范围内更改变量的特征。
% e8 U) h5 ], L& W/ o, U$ ?( W7 J' ~) x! ?9 M
它还可以对变量进行速写声明。最后,它使你可以访问变量。) V ^; X& _; s1 [ R/ V* H
7 Z3 g6 x+ b ~& V
declare -A 创建一个 associative array 变量,一个键值对数组,其值由关键字索引。
# ?1 ]8 \7 ~0 L% j4 `4 d4 }! M4 M) p( U" L$ D: P
除了影响其行为的变量之外,还可以为 Bash 函数赋予属性。1 D) p v5 d9 |, D! Z! T
8 X$ D* f2 B. c* [+ g! P9 {
declare 命令的语法使用 Bash) Q0 | ~0 m5 d. `% l5 p8 ]- b
$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]
5 s# x1 J" u4 [7 p0 g [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] ...
- S, B- M5 N* z' d选项作为 Bash 中 declare 命令的一般参数8 T5 Y# y) v: @. V
declare 内置命令接受以下选项作为通用参数:! _4 m2 l8 M( O9 h& _& e
( R1 c/ A- P8 a7 }9 L$ bash -c "help declare"
( Q0 n5 J' y$ ]输出:3 c Y5 N; w2 E+ J f# [
9 S; G: f9 T5 A* Zdeclare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]0 ?% r. z& V9 O! n# }
Set variable values and attributes.
; e" D' d& c; S y7 p. Z: r F: M Declare variables and give them attributes. If no NAMEs are given,
% j- } K" V, _9 o1 |3 D# i# } display the attributes and values of all variables.% g$ e; e$ U V2 A/ O
Options:8 O# t6 o O m' d+ b2 ^2 S! C. b
-f restrict action or display to function names and definitions
. @2 z# f8 U& p: N5 h -F restrict display to function names only (plus line number and' @3 M0 q1 d1 }# p! U, e; Z
source file when debugging)
; |% ^ g7 b& t. v t -g create global variables when used in a shell function; otherwise% J0 |$ I3 S7 i, [
ignored
% r+ p' y) [% X: a7 W: Q* ] -p display the attributes and value of each NAME/ r: w' J% M6 Z, x
Options which set attributes:
; S: I6 B* z4 F$ m -a to make NAMEs indexed arrays (if supported)
4 V* ?. k' j Q3 n7 _ -A to make NAMEs associative arrays (if supported)! K( O7 y2 d, |2 U, K& E o
-i to make NAMEs have the `integer` attribute* @, W! N- d! L* D
-l to convert the value of each NAME to lower case on assignment( J3 H7 R" F. L1 F' K, k( j1 E+ k
-n make NAME a reference to the variable named by its value
$ j1 `5 O! q/ ^/ K -r to make NAMEs `readonly`) G3 K+ w' g v) i& g9 B( ^1 x
-t to make NAMEs have the `trace` attribute' U) B( D; o% w
-u to convert the value of each NAME to upper case on assignment
1 S; K* Z; Y m8 S f$ u R* N -x to make NAMEs export
8 r3 N" K# k& T Using `+` instead of `-` turns off the given attribute.) q; |" f) X& o; y1 @
Variables with the integer attribute have arithmetic evaluation (see. N: p- z4 K. n. R6 A
the `let` command) performed when the variable is assigned a value.
0 M. E$ Z. S" T When used in a function, `declare` makes NAMEs local, as with the `local`
' ~1 T% D/ V" v+ G- e. k command. The `-g` option suppresses this behavior.
! R6 K: o( e* u" R" M1 z Exit Status:! ~; h+ S2 _9 b8 C9 j6 g1 e4 j
Returns success unless an invalid option is supplied or a variable
# `, ?1 g* q8 C( e0 \) q% [ assignment error occurs.
$ S$ _8 R# \- T1 S$ o2 g1 M( X0 L下面是一些 help 命令的示例,以查看它们在终端中的显示方式。值得注意的是,最后一个对于我们的 Windows Git Bash 用户来说是一个故障保护。% c5 B% f. J* {- k$ `7 c
0 D2 P/ g, K L; P$ o
现在你已经阅读了 bash declare 的介绍和手册页,是时候查看一些使用 bash declare 的实际示例了。
: C- \' U2 F& u: Z5 c: m8 B+ |9 v" Y$ p' J0 }2 ]* L( d/ B
Bash 中 declare 命令中使用的整数类型" Q( c) _- [% K! [' L6 X) s+ @0 \
使用 -i 标志将变量声明为整数。你可以在下面的示例中看到变量 age 被指定为 integer 类型。/ `& D6 v0 g0 {/ g2 x; G6 ?
1 T5 r6 @! J% T7 r* [
当你分配不同类型的字符串值时,你可能会看到一条错误消息,上面写着 Not the correct type;而不是抛出错误,错误变量将设置为零。
; `( {# i+ V# J# M. X0 k
% E0 T5 u. ^3 V3 C1 D2 K4 r$ declare -i num=7( {, {7 D" E g7 p' M
$ echo $num
n$ Q7 H1 Z f9 x; |: C输出:
9 i" q5 o2 q U) ~5 `/ e0 t. F- V6 D0 }; K5 r1 X
71 r& E7 C4 p( ^8 ?9 ~% ?
结果,将整数类型重新分配给字符串会产生 0。6 r1 A" S' o4 D% N, U
( I, T$ @% ~4 [, m
$ num=string' X3 c1 V" D: M( Q( R" i* R
$ echo $num
0 F1 U/ z) ?# T" X# y输出:+ R6 U: K4 o" g" c
6 l- n3 k* F0 V- y' g8 j
0
! J; Z7 N% z% l! i; j在 Bash 中声明只读变量
: a$ C4 ^% h7 Z4 z. P) e; t这个编码词描述了一个变量如何被赋予一个值并且不能被程序员或机器修改。它在整个程序的生命周期内保持不变。) ]7 n7 y! S: D6 g0 ]$ k+ x
9 O) f* P& x, R5 W2 ]! L/ U. `
使用 -r 标志使你的变量常量为只读。
3 O; X- l! ~4 J
( e# Q5 E* C- U5 I( p5 s9 n$ declare -r num=7
/ W' W( |! ?% z: H2 L1 H3 ^输出:' C& P4 k' Q8 M$ n/ x7 Q3 S
( A# E: s# x0 f$ `& Lbash: declare: num: readonly variable6 t! s d* i; g3 s1 L8 f4 n
如你所见,输出结果显示 readonly 变量。! q& M- T/ x- S- p5 j8 q
0 V9 O- r& J, @- j y5 D在 Bash 中分配变量时使用小写和大写转换; }% b& ~( B& L' f
将变量分配给 bash 时,可以使用 -l 小写和 -u 大写标志将其从小写更改为大写,反之亦然。
( B% _1 L, N1 f% E* E
1 g2 v. c! A; O7 A$ j' T" j' w0 N$ declare -l name="THANOS"6 Y- A5 U* ~- I# u
$ declare -u name1="thanos"7 O) _* E1 V; K2 U0 I3 L
$ echo $name $name1) `9 {$ z, e# m6 b- A
输出:. E) `, \; q& s4 V
- L4 [+ Y* S; A1 C) ~" Wthanos THANOS3 U8 K, @: B# z" b J( u2 R" b) K" \5 o
在 Bash 中使用 Subshells 导出变量1 @" k* O- t6 O* M
如果你以前使用过 bash,你可能已经注意到人们使用 export 命令将声明的变量导出到脚本或 shell 会话中的子 shell。我们可以用 declare 命令做同样的事情。% v% R, o6 Q0 W6 l
/ _6 q( M Z# D( I' r-x 标志应该用于导出,而+x 标志将阻止属性被导出。* }1 j$ C( g8 d. a
/ y1 @6 g) Q% Y8 `9 v: x' z
$ declare -x name=thor% X% E! q& W' j; @0 \8 [7 G. [7 S: K
$ sh -c "echo $name"- p; L0 E. i% ?
输出:
# ~. s1 Z& v! s( T w/ K
6 p0 P0 m! x- Y$ W/ P! fthor( s( j) k$ t% w% m, U( g4 B7 c9 J
在 Bash 中检查是否指定了任何属性
; J$ Q$ t* y; g2 _& _5 z) s2 T使用 -p 标志,我们可以查看属性 variable 是否已定义。+ }' \3 a( J/ H$ f4 e7 R
V9 l: h- \0 e5 l1 e+ T: w7 U$ a=5;b=6;c=7
; a/ T. J! O _* t$ declare -p a b c
, W1 [* J. D' D- p输出: E4 y6 R% M! v- \# L
) `9 }; p M" ^declare -- a="5"1 u& F. ?3 c. ?" s1 |4 g: t
declare -- b="6"
6 T0 ?. ~, c2 T1 h' ~declare -- c="7"+ r+ I0 U* W6 g- ^0 N. j+ i
指定关键字后,将有两个破折号 --。声明 -a 数组。声明 -n 数字。它用于显示变量的类型 nameref。. f& B* r) h F7 Z. k+ K1 C
4 p: [* ]% }3 I9 N Q( s3 L
如果没有声明,它只会显示。( a+ c( T/ y) [0 P5 Q8 ~
8 f, L+ q0 z, Q检查 Bash 中的函数定义
' W! \4 _0 v1 s& Q/ T' r-F 和 -f 标志可用于检查函数是否已声明和定义。我正在制作一个简单的 inevitable_thanos 功能。# D \: Q5 _: @* f
1 n* x) E3 J" ]( V8 n! g' i8 s9 h
$ function inevitable_thanos(){ echo "ironman"; }$ @% v z( W3 j6 {% C6 ?
$ declare -f
: b. y$ g/ E6 n3 S2 \0 D输出:
2 F9 K6 a! z* C3 n, u9 f! Q: q$ d( C6 {: L
inevitable_thanos () r7 c6 K: d( F
{
2 o1 d; K- m) D1 T; Q: b, I& t echo "ironman"
5 Y0 z) I: l4 s6 Z" [2 N}
: {9 v* o' w9 _0 dquote ()
6 m- H$ p! p* h# b0 @
3 E" k0 l$ X# h& |4 ~ |
|