易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 387|回复: 0
收起左侧

shell 环境中 Bash 中的 declare 命令

[复制链接]
发表于 2023-3-26 11:00:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
declare 是一个内置的 Bash 命令,允许你在 shell 范围内更改变量的特征。0 A* X. }2 _& b6 m" h( [
) F( X; {' t- a5 e- d; F
它还可以对变量进行速写声明。最后,它使你可以访问变量。
) ?* O7 Q- v1 u% W0 S+ u- q" K$ v8 S1 X5 o% d
declare -A 创建一个 associative array 变量,一个键值对数组,其值由关键字索引。
+ {, n$ D6 v% m6 Q/ [4 Y; \5 K# k& j
除了影响其行为的变量之外,还可以为 Bash 函数赋予属性。
2 ^, Q2 ?( n' p8 O5 W! I1 }) r
! G. \* ~  W3 l5 \! h4 {2 qdeclare 命令的语法使用 Bash% ~( |* i& n0 _( E/ L; _
$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]  @: x0 \$ v' L- y) z; e( O
        [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] ...
  v! A! z7 b1 t3 j3 Q选项作为 Bash 中 declare 命令的一般参数# k3 _. B# ^6 i" Z/ K( x5 J& R
declare 内置命令接受以下选项作为通用参数:
1 R% y# k$ {" M. F" B5 U8 p6 ~5 K8 H' _5 n8 J  E" \$ U$ S8 H
$ bash -c "help declare"" {) B4 i( f2 N' g3 z# W
输出:
; ^) O/ L* j6 @& w& k2 {8 f+ Z. l' g3 M# k) F9 y, _' o
declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
) Z0 q$ h+ I; A% ?6 k( y/ D    Set variable values and attributes.
; I; A$ H$ i  T! x    Declare variables and give them attributes. If no NAMEs are given,: z. w" T7 R! }; Z
    display the attributes and values of all variables.
7 t3 Z% [" m% e3 e  e- C! a/ I    Options:
2 U9 c6 T7 O0 d- W; i1 s      -f        restrict action or display to function names and definitions
; O+ |- ~* k  B7 Q% N      -F        restrict display to function names only (plus line number and7 Q% ]9 f; Z( t, U5 z) c
                    source file when debugging)  r6 h8 \+ [7 g  t/ n
      -g        create global variables when used in a shell function; otherwise
" @3 U- E! U( _2 e  b. E                    ignored
0 X7 ~# {3 p- i0 p2 T      -p        display the attributes and value of each NAME4 U0 z7 {, H6 o& W0 i" V# M( X
    Options which set attributes:
( h* y" ^& ^( \  U/ P* g' J; d      -a        to make NAMEs indexed arrays (if supported)% i. w- |0 E! B$ F- ^6 M
      -A        to make NAMEs associative arrays (if supported). w  L9 }: L' s7 c6 c4 P
      -i        to make NAMEs have the `integer` attribute
, g* f; k4 c% x3 J# X/ n1 Q1 h      -l        to convert the value of each NAME to lower case on assignment
2 K( Z' y3 E$ X& o! q      -n        make NAME a reference to the variable named by its value, e8 \. d0 c7 `
      -r        to make NAMEs `readonly`) C, n# [9 h5 H+ @3 A( \8 J' Q
      -t        to make NAMEs have the `trace` attribute9 ~' l& H$ K* y- ~5 g7 ]
      -u        to convert the value of each NAME to upper case on assignment/ D* ^. k6 U, L  U2 S: ?$ j0 J
      -x        to make NAMEs export, F, H+ U0 n, H
    Using `+` instead of `-` turns off the given attribute.0 J' n2 c; K4 j% C
    Variables with the integer attribute have arithmetic evaluation (see' \3 }- E: J0 c$ ~' A+ n$ u
    the `let` command) performed when the variable is assigned a value.
1 `! e5 S! G7 N+ _! W    When used in a function, `declare` makes NAMEs local, as with the `local`
) E$ ~; x, d- y+ j. p; w    command. The `-g` option suppresses this behavior.
' V) z0 Z5 h2 ^7 _% s    Exit Status:2 m4 Y( l  E  T. s5 H. B/ z
    Returns success unless an invalid option is supplied or a variable
; m" ~/ O! Y# [    assignment error occurs.
$ }7 j3 F; x" o; t4 @4 [$ b. T下面是一些 help 命令的示例,以查看它们在终端中的显示方式。值得注意的是,最后一个对于我们的 Windows Git Bash 用户来说是一个故障保护。8 u/ q: u9 t/ T2 x2 ^! M
+ V* v2 y% v& ~, t5 Z+ r0 D* A9 G
现在你已经阅读了 bash declare 的介绍和手册页,是时候查看一些使用 bash declare 的实际示例了。# K1 W  |% G: ^+ m9 ^
6 p3 M9 D( H+ ~6 S- h3 B
Bash 中 declare 命令中使用的整数类型9 w6 i) e" C& O+ O. W" r0 L6 Z3 W
使用 -i 标志将变量声明为整数。你可以在下面的示例中看到变量 age 被指定为 integer 类型。3 y4 ?+ f8 {& Z5 r

; I$ J4 }- d: |$ a当你分配不同类型的字符串值时,你可能会看到一条错误消息,上面写着 Not the correct type;而不是抛出错误,错误变量将设置为零。
8 c1 Q$ \: G( G
7 ^/ l5 r$ O* Q/ L$ declare -i num=7
$ ?! i* s5 J* \1 d3 {$ k$ echo $num/ P  g0 o+ U; ~/ Q$ q
输出:
% k6 q$ D; c( p2 U2 _8 N5 r! S
, X* J% N9 v! F2 O4 X7$ N9 |- [! E5 h" i( s" e+ }
结果,将整数类型重新分配给字符串会产生 0。! F" U) p& N# c- K; f* d
& [4 H" D- ~; v% D7 Y
$ num=string& v7 V, n1 S2 u2 ^* a' p+ N7 `
$ echo $num
& z' f$ c5 Y# G  k" S输出:* N5 k/ e/ F- |* M; o: L

: D2 Y7 J$ {) \# e6 V* m) A0
- L6 k5 ~, V/ i+ {. `7 h% {/ x2 r在 Bash 中声明只读变量
! e' `9 ]: Y" ]. G# E这个编码词描述了一个变量如何被赋予一个值并且不能被程序员或机器修改。它在整个程序的生命周期内保持不变。3 l- ]) I3 H% \7 [- i

7 K7 y' i4 _, `* P" j使用 -r 标志使你的变量常量为只读。% d) @" m* e2 s' F

/ N2 [! u! x: A5 I$ declare -r num=79 z) V4 ?" @7 r0 v  s  m+ A
输出:3 i& |+ a! Z" U3 k" R3 F5 Q
  q: d- h) A; P4 x$ V2 B
bash: declare: num: readonly variable/ Z3 e0 S8 I6 k* I  Y5 c
如你所见,输出结果显示 readonly 变量。3 i, E$ _6 E# S: B+ F4 J' t
4 Y* {6 [3 l. s
在 Bash 中分配变量时使用小写和大写转换5 y9 z  k9 t! T7 I1 P
将变量分配给 bash 时,可以使用 -l 小写和 -u 大写标志将其从小写更改为大写,反之亦然。
: h3 C$ }! N, a3 V1 ^. U, m- C2 x* O0 }, A( E
$ declare -l name="THANOS"! D3 [4 ~6 B* A/ V
$ declare -u name1="thanos"
8 f8 y( |2 l) y; l4 W) Y$ echo $name $name1  t6 g# U+ ]' m6 \; J( X& x, Z
输出:
+ z6 Q$ N+ k+ ^( W' T! e. O6 ?3 W7 N  |( K# o. l
thanos THANOS% h) j/ i7 `: B& p, C* ^
在 Bash 中使用 Subshells 导出变量% G& B: E, K* N1 @( w0 U9 F( w8 f
如果你以前使用过 bash,你可能已经注意到人们使用 export 命令将声明的变量导出到脚本或 shell 会话中的子 shell。我们可以用 declare 命令做同样的事情。) W. y. P* S# V) |; L8 n% r
9 ~- W( v7 J& @7 c" t! V
-x 标志应该用于导出,而+x 标志将阻止属性被导出。
  P2 O: d2 P# t7 x- Q" m( y: S. l5 \7 D: e( C
$ declare -x name=thor5 f1 ^& L; c. \8 T3 m0 B0 J8 x
$ sh -c "echo $name"8 e# d6 t2 s6 |' I
输出:
. |/ A% f! [/ O' U; b
3 ^. V+ U, {4 |9 {% F2 {9 w, Uthor
0 {1 ?& i% V$ t1 }7 z+ f5 b- G! Q在 Bash 中检查是否指定了任何属性
% m3 c$ i5 o- C使用 -p 标志,我们可以查看属性 variable 是否已定义。! X2 \( Q! i- M9 t- v+ W) M6 |

" R- W' }, L: x) z% z) L$ a=5;b=6;c=7
# M; F; O+ x2 B$ declare -p a b c
$ ]( e) g4 b$ r4 J1 q8 E" e9 x输出:
! r! _+ o9 P. _3 ]2 }$ e2 q  ~1 Z# P# M8 r+ z
declare -- a="5"4 n- M+ ~; \8 J
declare -- b="6"
# [" B0 W  |) y2 j, n6 ^) Ndeclare -- c="7"+ Y# ]) J8 q% X/ G1 o
指定关键字后,将有两个破折号 --。声明 -a 数组。声明 -n 数字。它用于显示变量的类型 nameref。" P' ^& m# ?1 s# f

7 g# M* q* p6 }! w. C如果没有声明,它只会显示。% m$ w+ C- X6 u* Y9 \! G
6 u6 p) B! J5 f9 I
检查 Bash 中的函数定义
: e2 p$ D/ y  h. C0 J0 ^-F 和 -f 标志可用于检查函数是否已声明和定义。我正在制作一个简单的 inevitable_thanos 功能。
0 A" ?8 e# j" \& U  u. K; d$ A2 Y* \# q8 z. y5 t/ ~
$ function inevitable_thanos(){ echo "ironman"; }
% C6 F; x! W* t2 f$ declare -f9 b: t; I: L. Q+ ~3 t. ?' i
输出:5 M9 f" T5 v3 T$ G
8 T1 G: m$ |  _  c4 U# a& |
inevitable_thanos ()
9 G/ O! r9 o8 k5 _" k{0 {! N- V& o5 a) Y( G
    echo "ironman"
. Y8 D: _. q8 x! F}
) \& i  S( F) A; |" ~6 Fquote ()* o/ Y* D3 ~' ^. }1 W: s: Q. f+ ~
% o7 i# B# F' f$ {# z
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-9 01:11 , Processed in 0.046647 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

快速回复 返回顶部 返回列表