有什么作用
declare name
Run Code Online (Sandbox Code Playgroud)
不提供任何选项吗?它是否声明了字符串变量的名称?
有什么作用
declare -g
Run Code Online (Sandbox Code Playgroud)
不提供变量名怎么办?它是否显示具有全局属性的所有变量的值?
我declare
在 Bash 手册的描述中没有找到答案。
谢谢。
在阅读了Kusalananda 的这个答案后,我对什么是变量属性有了一些了解,但我想念什么是变量属性的“名称引用”(类型?),它的用途是什么,为什么人们想要在 Bash 脚本中使用它,
我尝试用 Google 搜索术语“名称引用”(不带引号),但没有找到有关该术语的 wiki 文章。
我的目标是理解“变量属性”的一般概念,希望它能帮助我理解Bash 中的声明。
什么是可变属性?为什么有人要为变量赋予属性?为什么在使用变量时不只是创建一个变量并在执行中扩展它们就“足够了”?
declare -r is_debug true
if [ "$is_debug" = true ]; then
printf "%s\n", "is debug"
else
printf "%s\n", "is not debug"
fi
declare -p is_debug
echo "${is_debug-isunset}"
Run Code Online (Sandbox Code Playgroud)
输出:
jian@jian:~/Desktop/pg_src/src4/postgres$ bash /home/jian/scratch/scratchpads/47a6066b9dfaca1c2b13ea2e99f4e929/scratch.sh
is not debug
,declare -r is_debug
isunset
jian@jian:~/Desktop/pg_src/src4/postgres$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, …
Run Code Online (Sandbox Code Playgroud)