易陆发现互联网技术论坛

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

shell 环境中 Bash 中的 declare 命令

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

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

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

x
declare 是一个内置的 Bash 命令,允许你在 shell 范围内更改变量的特征。
2 o4 R  M% \1 l, }' c0 a, L# K5 ~% i$ y4 F! x3 U
它还可以对变量进行速写声明。最后,它使你可以访问变量。
: E' T4 |; v& H& @) X  p2 @- e
) k7 S, o& Q' \0 k( _7 b. x, Ydeclare -A 创建一个 associative array 变量,一个键值对数组,其值由关键字索引。
; ], g3 |5 b7 R" D8 G+ S& E& ~8 m( s9 y( w
除了影响其行为的变量之外,还可以为 Bash 函数赋予属性。
; e! ]% Q. M, J" ]
7 t( |- F, l  Ndeclare 命令的语法使用 Bash
4 i( M' i8 g: n! z$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]: ]) r0 w7 _2 S) B
        [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] .... E' j' C7 d, [3 i9 @# c( v' x
选项作为 Bash 中 declare 命令的一般参数2 K  I- B) m, X( R: Y' J! g( j
declare 内置命令接受以下选项作为通用参数:& U$ x8 A) V* c0 B

$ e& o1 |  R$ G, L$ bash -c "help declare"6 T  e, a- X8 b! Z! ^
输出:7 O) B8 I$ ^2 W  @3 ]

; e& n5 w: Y! o; z+ |* Ldeclare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]+ }" ]6 F2 p- X' [( Y& x2 o
    Set variable values and attributes.% v# [2 S: U) A
    Declare variables and give them attributes. If no NAMEs are given,
/ v; k! o" B. _( `1 V1 F5 m    display the attributes and values of all variables.: z  e3 N+ s& x3 l! q$ W: s$ ~
    Options:! ?* i& m" G! g+ D
      -f        restrict action or display to function names and definitions
) ?+ N7 s/ L- ]0 G/ t      -F        restrict display to function names only (plus line number and+ h9 p4 ^; `' H  A2 U8 P. S9 Q7 x' W
                    source file when debugging); L' m  G6 {5 E, G# {3 M( ?
      -g        create global variables when used in a shell function; otherwise
  h8 |5 C6 ?' l+ F                    ignored' |* Q4 i; D6 r" n; U: Q. ?. `! \
      -p        display the attributes and value of each NAME* L5 z3 M" _4 [' {) W9 ?
    Options which set attributes:
* [+ W: ~) X4 M% @+ {      -a        to make NAMEs indexed arrays (if supported)
5 w' W5 P& C/ s- G" F      -A        to make NAMEs associative arrays (if supported), i( Q0 k1 e# e/ a2 G+ ]
      -i        to make NAMEs have the `integer` attribute0 x9 g& C& d/ ~" w% m& j( g, e
      -l        to convert the value of each NAME to lower case on assignment4 K& z, b& V* k/ G9 W: G$ t
      -n        make NAME a reference to the variable named by its value
) h# o' M) V. X! T! q5 P/ P' J      -r        to make NAMEs `readonly`
! H4 F7 _2 \+ I8 v6 }+ V      -t        to make NAMEs have the `trace` attribute6 P8 g! J0 ?4 t% C7 ?4 R
      -u        to convert the value of each NAME to upper case on assignment
) t* t% e4 S8 T3 ^. ?" f5 \      -x        to make NAMEs export
& _8 h( I8 z! Y- r+ C# w! v( @    Using `+` instead of `-` turns off the given attribute.6 ?# Y7 X4 g( ]& R4 W7 D
    Variables with the integer attribute have arithmetic evaluation (see" K: _0 F" N. K% c3 Y, y
    the `let` command) performed when the variable is assigned a value.
9 {) d8 T) k" H* C    When used in a function, `declare` makes NAMEs local, as with the `local`: U6 f2 q: z' B- b6 g1 @' s6 x
    command. The `-g` option suppresses this behavior.; L& K* K9 E8 ]# x7 T
    Exit Status:
- B0 `: b  C5 L/ B    Returns success unless an invalid option is supplied or a variable
( f. i: h4 L( m    assignment error occurs.
2 B+ C4 N* \. I4 \下面是一些 help 命令的示例,以查看它们在终端中的显示方式。值得注意的是,最后一个对于我们的 Windows Git Bash 用户来说是一个故障保护。
0 {! u% T$ i0 D9 }. h
! v4 C, Y' M" W( l2 ?现在你已经阅读了 bash declare 的介绍和手册页,是时候查看一些使用 bash declare 的实际示例了。2 B4 G: g5 _; X- L# d; }

5 _& V; b+ M: F9 M' PBash 中 declare 命令中使用的整数类型$ k- n$ r; T3 }' g, a  L2 W- Y
使用 -i 标志将变量声明为整数。你可以在下面的示例中看到变量 age 被指定为 integer 类型。( I- s7 E7 _' O$ f" W: T! {
. l+ b- x# m8 g* V( j
当你分配不同类型的字符串值时,你可能会看到一条错误消息,上面写着 Not the correct type;而不是抛出错误,错误变量将设置为零。
2 q$ F, x) d) n0 g
' I& \- s' H5 d; p2 @: t! J4 }$ declare -i num=7
/ @9 E8 q1 U+ E5 h% t8 F% _7 h$ echo $num1 u- L: U+ D& J3 a  H" Q4 K7 d' E
输出:
3 j- A! d' U$ f8 U9 e9 S
% z0 E  U! z$ `+ V) e7
( i2 v( H: e8 W" _/ S+ C4 _3 l  Z结果,将整数类型重新分配给字符串会产生 0。, u: [0 H( `" J
- r; |# k/ F5 S  L
$ num=string" U8 R1 B9 e+ C% u1 K, y3 o
$ echo $num1 _7 e# r# l# G0 L% a
输出:6 |- L% l9 M. w; s" a; t* \$ S& b

3 ?' J5 n3 D. c* f6 F0! c$ q2 l  |' v9 z0 Q
在 Bash 中声明只读变量
4 d* x* H- E* L" @! @6 ?0 h这个编码词描述了一个变量如何被赋予一个值并且不能被程序员或机器修改。它在整个程序的生命周期内保持不变。
0 d9 ^* I: R3 t% R1 L2 l9 C8 t8 H
& \. W, q5 [. G7 Q- i5 G使用 -r 标志使你的变量常量为只读。
) V* w  c6 J: X
9 ?8 a4 \9 \# S" ?2 L$ declare -r num=7
0 C1 Y& c9 i8 w/ s输出:2 u: x$ L6 [. r) |
' e1 }7 a% e  O; b. d- G
bash: declare: num: readonly variable
# V6 F; B. j( K& |( o5 n5 x如你所见,输出结果显示 readonly 变量。7 o: {  h0 J$ c

( y2 q  ]% {# G0 x  @% k在 Bash 中分配变量时使用小写和大写转换6 H- ^/ g0 y9 D4 k
将变量分配给 bash 时,可以使用 -l 小写和 -u 大写标志将其从小写更改为大写,反之亦然。$ G- S4 n$ G) g; c- j& p

  P: ]$ |# j6 Q) Y. f$ declare -l name="THANOS"
: w6 \9 f* U# J5 S$ declare -u name1="thanos"
9 a- T0 P) {: a  \0 A7 C8 P( D$ echo $name $name1
. S" t' s3 A! v" @  B2 Z4 j输出:
" Q+ c4 O0 r* X
/ ~2 g8 m6 o& h# Z+ X0 N1 s% `thanos THANOS$ D/ R9 H2 s) S) q6 \
在 Bash 中使用 Subshells 导出变量+ |* }* e3 `. M/ |" g% I& {$ i: Z/ w; C
如果你以前使用过 bash,你可能已经注意到人们使用 export 命令将声明的变量导出到脚本或 shell 会话中的子 shell。我们可以用 declare 命令做同样的事情。
, p3 `& ]1 ~. k3 J6 r! d* W* F; y
7 b$ p  p7 o4 N# e- w: p-x 标志应该用于导出,而+x 标志将阻止属性被导出。3 b# `$ ~9 w* b6 L- Q. |7 C" N8 f4 E) M

) c* _/ I4 @' z2 m& t7 G$ declare -x name=thor; h; n3 x7 m$ @- o+ A2 T
$ sh -c "echo $name"/ g, h1 v3 ~. V' @9 N
输出:
) S' b# i7 I! W( y* G
/ s7 ], @; U/ Y: S# othor
4 i1 k" A' B# F- e5 d4 A( j在 Bash 中检查是否指定了任何属性
- r3 a; {2 ^! e- @: r使用 -p 标志,我们可以查看属性 variable 是否已定义。1 [- x2 A, q7 R, d

9 ?, N' Q; o0 d7 ~* \6 t$ a=5;b=6;c=7
; f; Y/ w1 |# E( y0 c% k$ declare -p a b c
! F9 f6 q6 s. p/ W输出:& n1 Z; @- g9 G
) V" e, v* L5 u# M" F1 R
declare -- a="5"1 u# m1 B. V/ j: c# z8 L
declare -- b="6"
6 \) |7 n' U$ T% Hdeclare -- c="7"
8 A) x( W$ X' X: _3 s指定关键字后,将有两个破折号 --。声明 -a 数组。声明 -n 数字。它用于显示变量的类型 nameref。
0 _  n% ~3 u, R. \! Y) D% P
$ a2 y8 I, W/ w8 K2 g如果没有声明,它只会显示。
5 u) c/ p8 T$ g, Z+ ~, Q% v9 X/ m3 g. r" v
检查 Bash 中的函数定义
# i7 j# K8 f) Z* g-F 和 -f 标志可用于检查函数是否已声明和定义。我正在制作一个简单的 inevitable_thanos 功能。
( S( ?3 v2 E' Y6 o( g' Q
$ W, i+ c) `; H  z8 E3 o$ function inevitable_thanos(){ echo "ironman"; }
! R2 t2 o3 v6 n# W  U) {) V% B$ declare -f3 b$ p5 k& U- n" p) f! o- [9 j/ ^9 x! Z
输出:
$ P  T: f, d+ S% G
# o; f% X& Z, V* v6 ginevitable_thanos ()
, N! Z( [1 @/ t) D9 Y. m{
9 F5 I* O+ B& K1 n( l    echo "ironman"
# b( K% P* J/ J) W6 m. E# W; O}
' \, \5 o2 n" f7 e' E& Oquote ()
4 s! V5 x* F' x" J/ G9 I' N( h6 s. i( e1 ~9 l2 F. O8 D
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

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

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

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

GMT+8, 2026-4-8 21:37 , Processed in 0.048050 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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