找回密码
 注册
查看: 390|回复: 0

shell 环境中 Bash 中的 declare 命令

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-3-26 11:00:05 | 显示全部楼层 |阅读模式
declare 是一个内置的 Bash 命令,允许你在 shell 范围内更改变量的特征。
* k. Y, k5 \8 v' x% m- n
# E; i' f! n+ m# Y" E( |它还可以对变量进行速写声明。最后,它使你可以访问变量。- w. ^0 F4 r6 l6 z: K
) o7 Y7 ~6 ]0 v% f% J
declare -A 创建一个 associative array 变量,一个键值对数组,其值由关键字索引。/ d4 [) \) y8 X1 s7 o

( h; ^2 A9 I# s" |4 `$ ?除了影响其行为的变量之外,还可以为 Bash 函数赋予属性。# Q& L5 f+ U, _: s9 x- J

' i% N/ J  T" d* `/ B2 N* {declare 命令的语法使用 Bash' V$ P  M( X/ Q& k2 ]& i- f
$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]
4 d- T1 E# l  l        [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] ...8 I2 w$ T2 l: r) F
选项作为 Bash 中 declare 命令的一般参数& d1 Z. L. ^: y! F1 p
declare 内置命令接受以下选项作为通用参数:3 [/ Z9 D6 |/ f& S) @) V) U

1 {+ T9 n$ n1 }* P/ {) n3 e$ bash -c "help declare"; F4 v- H& B# p* @6 N- D$ d
输出:) [; M8 w3 \' i% k5 S+ c$ [

7 y# C; ^0 Y0 K' t3 n& wdeclare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
8 `# ^2 o' Z: ~0 i    Set variable values and attributes.
: x& g8 ]+ ^" h- ?9 t    Declare variables and give them attributes. If no NAMEs are given,+ R+ ^6 {" U  P6 K/ b
    display the attributes and values of all variables.
+ H+ v& ^. S) I5 i$ |9 x    Options:
8 o# g+ t8 R0 Q% j! ]0 k( c" y, E. i: C      -f        restrict action or display to function names and definitions
( Y2 h- D1 h8 ]& E- R      -F        restrict display to function names only (plus line number and
2 J/ r$ K# U6 l/ \; u/ n5 [, i% Z                    source file when debugging)$ X3 m- K4 q/ R
      -g        create global variables when used in a shell function; otherwise
& G. H8 q; b/ v5 K1 U                    ignored8 R+ L% T1 y( Z5 F) \1 Z2 u6 W; d
      -p        display the attributes and value of each NAME
/ @# G; J6 P3 |) u+ W: g, l3 I9 q    Options which set attributes:; k# ]/ n$ }' v7 Z
      -a        to make NAMEs indexed arrays (if supported)
& \+ J8 A* B* [$ r3 h' b: ?      -A        to make NAMEs associative arrays (if supported)- j5 @" L/ c: G1 f3 F
      -i        to make NAMEs have the `integer` attribute
8 W+ y, o7 g: @1 W7 f      -l        to convert the value of each NAME to lower case on assignment
/ R% B$ @6 K: \4 d# I! e      -n        make NAME a reference to the variable named by its value# Y) w3 M1 Y! Y9 |
      -r        to make NAMEs `readonly`. \2 b) k: b: u% I
      -t        to make NAMEs have the `trace` attribute5 {; n4 Q6 A9 _! r( y% e
      -u        to convert the value of each NAME to upper case on assignment, _. z2 [% D# K& x8 }. y5 g
      -x        to make NAMEs export# b1 V% ]& U( G( b
    Using `+` instead of `-` turns off the given attribute.6 b# w# H" G( v' f' U
    Variables with the integer attribute have arithmetic evaluation (see
. E: b8 c2 G5 E    the `let` command) performed when the variable is assigned a value.& r3 r+ n+ o! T( z) ^
    When used in a function, `declare` makes NAMEs local, as with the `local`% k1 z/ d& b5 f5 d; n
    command. The `-g` option suppresses this behavior.8 J" D; P+ `+ G+ O" J
    Exit Status:7 v7 B+ D6 ?' t; T/ u5 J
    Returns success unless an invalid option is supplied or a variable* B- i( {3 S. c+ T6 I
    assignment error occurs.( y6 m3 b( R8 }- H) ~
下面是一些 help 命令的示例,以查看它们在终端中的显示方式。值得注意的是,最后一个对于我们的 Windows Git Bash 用户来说是一个故障保护。
* `6 C- `* ^; z
/ W# E: t1 j+ G现在你已经阅读了 bash declare 的介绍和手册页,是时候查看一些使用 bash declare 的实际示例了。
+ V7 S* O: J8 I* x& V. t& q# J- b9 h/ [" j  Q! L: y; ]' h
Bash 中 declare 命令中使用的整数类型
& O; R4 V  Y! C* y6 H; c3 e使用 -i 标志将变量声明为整数。你可以在下面的示例中看到变量 age 被指定为 integer 类型。6 C* Q$ T, w' l2 h

6 h0 ^- t) l+ e& ~当你分配不同类型的字符串值时,你可能会看到一条错误消息,上面写着 Not the correct type;而不是抛出错误,错误变量将设置为零。1 M3 U- L+ A) K
3 t0 ?3 ~7 ]3 P6 c: L6 H8 y9 {
$ declare -i num=7& J+ X; m7 ?+ P/ H* [) y9 a: l
$ echo $num0 {& |: h- `* D9 {2 h0 K7 d# h
输出:0 [6 p- P9 I6 L( ?  i
& K6 Y9 S8 ]& C2 Y# F7 s
7
( \3 P/ t# K0 P! f1 k5 e. n结果,将整数类型重新分配给字符串会产生 0。' B9 P3 \% ^' {, l" @
3 C- U! R/ b* Q% B* Q
$ num=string$ B& r: W- T9 T. K. U# H
$ echo $num7 N# f8 B- [7 ^5 b" k
输出:
: [4 Y3 p7 X' v3 R( N  I0 O7 _+ @0 R" H0 f& d9 h" i
0
  _0 Q: x9 N. L1 ]7 u0 Y* V% N在 Bash 中声明只读变量
0 |0 ?2 H' R6 @1 l这个编码词描述了一个变量如何被赋予一个值并且不能被程序员或机器修改。它在整个程序的生命周期内保持不变。! A- E- e3 A: |& x5 _2 G: [

5 V# C0 e$ K$ N- `" m7 M% ~* S( _使用 -r 标志使你的变量常量为只读。0 K$ A; x, s5 A& w
4 y  ~/ t2 X% w6 c2 B
$ declare -r num=7
- D/ h/ q7 t3 ^5 N. M- u+ c! r; J输出:" j' I$ \" H& p

& U! h/ o0 y" y  F; Y+ ^bash: declare: num: readonly variable
6 o. {! P4 O/ ]: d: O' C如你所见,输出结果显示 readonly 变量。
4 R# t' M# I9 R' o% _1 F+ j5 \' T3 i9 v6 V. S+ v# r, ?
在 Bash 中分配变量时使用小写和大写转换
) ?  q5 R4 h; Z4 W! W. a将变量分配给 bash 时,可以使用 -l 小写和 -u 大写标志将其从小写更改为大写,反之亦然。
, r* c  E) l# \) Q* T  u/ @# q: v: c1 l7 f6 R0 f
$ declare -l name="THANOS"( x$ }4 S! Z, ?
$ declare -u name1="thanos"& O* ~& G& l8 u/ r( f& p
$ echo $name $name1' l! _2 z# i0 B$ M2 p
输出:# H0 c8 j" F5 A
, c8 U6 o4 @  {) Y( k
thanos THANOS2 Q- U7 W. O9 M& l- \/ n
在 Bash 中使用 Subshells 导出变量, \8 p! a/ e# X
如果你以前使用过 bash,你可能已经注意到人们使用 export 命令将声明的变量导出到脚本或 shell 会话中的子 shell。我们可以用 declare 命令做同样的事情。+ K! {) H2 J- u: W. r

/ W3 l- ]$ r; l( ?! z-x 标志应该用于导出,而+x 标志将阻止属性被导出。
3 [0 N$ `: k! W3 I3 w: `( u4 k+ ~( a* w" N
$ declare -x name=thor
% m8 G/ g7 z# G9 s$ sh -c "echo $name"
& |" X2 e$ C4 }1 U  {, H9 [4 A& W. w$ v输出:
* c8 ~( a% s5 {
5 i! G4 p. K( J4 q& Gthor
! i7 [% z7 b( c) a在 Bash 中检查是否指定了任何属性- f' x& v* a+ t9 V% x) B' @/ A
使用 -p 标志,我们可以查看属性 variable 是否已定义。
5 z* V: |: l7 U3 K( Z/ J4 N" O) p: `* Y
$ a=5;b=6;c=7
/ q. e' J0 _/ o# W2 k0 h! J; q$ declare -p a b c5 }( j( a0 h9 V  E3 b
输出:" c( O+ {- c6 T' d: ~3 \$ t  r

4 u% |1 K+ H3 t# ~& @+ F7 c0 Cdeclare -- a="5"+ D, d$ ~* I. _8 z3 Z, e0 F
declare -- b="6"
  o+ C% o/ g1 }; C/ Vdeclare -- c="7"
: h' y' t8 d9 `$ @1 G) o& F) e指定关键字后,将有两个破折号 --。声明 -a 数组。声明 -n 数字。它用于显示变量的类型 nameref。
$ s0 z; h8 C5 S/ s' G$ W4 j% c, K* H3 X2 a( b6 d7 Q
如果没有声明,它只会显示。
/ _7 x0 S4 f9 B6 ^( O! r
8 j, Y4 ]6 A/ X; |) B8 ^5 l检查 Bash 中的函数定义
" R* ^% W& b6 {$ [% R; x8 I* q* d0 f-F 和 -f 标志可用于检查函数是否已声明和定义。我正在制作一个简单的 inevitable_thanos 功能。9 Z# g$ ?( k- u/ X5 Z
, [3 Q& ?' S. `, l) s& b
$ function inevitable_thanos(){ echo "ironman"; }
5 u' p6 y4 d* F3 F+ J% {2 g. d$ declare -f$ _3 I% I6 W6 |) F, w
输出:. t3 w8 z3 j* e
2 A3 `4 i8 ~  L% j( [! S" V* Z+ e! l5 t
inevitable_thanos ()8 ]  e0 k2 {( D8 z6 Y9 Y: h
{' i% q. {  }$ o1 E
    echo "ironman"
9 O& S5 u  E) _! \; j: a}
& b/ R6 k/ b* E* S$ O7 Y5 kquote ()% P8 e' d2 b0 F2 @: I
) h. q$ ~" R7 t7 \" j  K7 E
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 01:45 , Processed in 0.017878 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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