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

  

最新文章

  1. Arcmap中加载互联网地图资源
  2. Win10 下安装 NodeJS
  3. reveal 1.6.3 本机破解及使用
  4. Cisco ASA 高级配置
  5. 【译】理解与分析ios应用的崩溃报告
  6. c语言背后的运行机制
  7. 面试时如何优雅的谈论OC
  8. EditText判断用户输入完成,然后进行操作解决方案
  9. 初识EF
  10. 再谈加密-RSA非对称加密的理解和使用
  11. 如何在centos7上安装源码包
  12. (详细)华为荣耀4X CHE-TL00H的usb调试模式在哪里打开的步骤
  13. Oracle错误——tablespace 'XXXX' does not exist
  14. elementUI中table组件会出现空白部分
  15. (总结)Ubuntu apt-get apt-cache命令 使用
  16. 浅谈STM32L071硬件I2C挂死
  17. E 定向 牛客练习赛25
  18. 对jquery新增加的class绑定事件 jquery 对相同class 绑定事件
  19. 实验三 使用ASP.NET常用服务器控件 总结
  20. BKL 大内核锁

热门文章

  1. java之集合概述
  2. Oracle 之 配置HugePages内存
  3. Oracle DB备份恢复篇之丢失控制文件
  4. socket初步
  5. ubuntu12.04安装KDevelop
  6. Micro-PaaS(Docker+K8S)
  7. mybatis 面试
  8. Netty--JDK序列化编解码传输对象
  9. 纯css实现点击事件
  10. Java实现动态加载读取properties文件