shell-array
2024-08-24 11:07:22
【shell-array】
Creating Array:
$ names=("Bob" "Peter" "$USER" "Big Bad John")
$ names=([]="Bob" []="Peter" []="$USER" []="Big Bad John")
# or...
$ names[]="Bob"
You can get the number of elements of an array by using ${#array[@]}:
$ array=(a b c)
$ echo ${#array[@]}
There is also a second form of expanding all array elements, which is "${arrayname[*]}". This form is ONLY useful for converting arrays into a single string with all the elements joined together. The main purpose for this is outputting the array to humans:
$ names=("Bob" "Peter" "$USER" "Big Bad John")
$ echo "Today's contestants are: ${names[*]}"
Today's contestants are: Bob Peter lhunath Big Bad John
Associative Arrays
To create an associative array, you need to declare it as such (using declare -A).
$ declare -A fullNames
$ fullNames=( ["lhunath"]="Maarten Billemont" ["greycat"]="Greg Wooledge" )
$ echo "Current user is: $USER. Full name: ${fullNames[$USER]}."
Current user is: lhunath. Full name: Maarten Billemont.
参考:
1. http://hi.baidu.com/gaogaf/item/46050b15958a5225f7625c4a
2. http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html
3. http://bash.cumulonim.biz/BashGuide(2f)Arrays.html
最新文章
- Arcmap中加载互联网地图资源
- Win10 下安装 NodeJS
- reveal 1.6.3 本机破解及使用
- Cisco ASA 高级配置
- 【译】理解与分析ios应用的崩溃报告
- c语言背后的运行机制
- 面试时如何优雅的谈论OC
- EditText判断用户输入完成,然后进行操作解决方案
- 初识EF
- 再谈加密-RSA非对称加密的理解和使用
- 如何在centos7上安装源码包
- (详细)华为荣耀4X CHE-TL00H的usb调试模式在哪里打开的步骤
- Oracle错误——tablespace 'XXXX' does not exist
- elementUI中table组件会出现空白部分
- (总结)Ubuntu apt-get apt-cache命令 使用
- 浅谈STM32L071硬件I2C挂死
- E 定向 牛客练习赛25
- 对jquery新增加的class绑定事件 jquery 对相同class 绑定事件
- 实验三 使用ASP.NET常用服务器控件 总结
- BKL 大内核锁