【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

  

shell-array的更多相关文章

  1. Linux, Mac下Shell 数组 Array 的修理工

    我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当 ...

  2. PHP安全编程:shell命令注入(转)

    使用系统命令是一项危险的操作,尤其在你试图使用远程数据来构造要执行的命令时更是如此.如果使用了被污染数据,命令注入漏洞就产生了. exec()是用于执行shell命令的函数.它返回执行并返回命令输出的 ...

  3. shell 3数组

    shell数组 shell支持一维数组(不支持多维数组),并且没有限定数组的大小. 定义数组 shell中,用括号来表示数组,数组元素用空格分隔,下标从0开始,元素的类型 方式1 数组名=(值1 值2 ...

  4. shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计

    shell编程系列21--文本处理三剑客之awk中数组的用法及模拟生产环境数据统计 shell中的数组的用法: shell数组中的下标是从0开始的 array=("Allen" & ...

  5. shell编程系列4--有类型变量:字符串、只读类型、整数、数组

    shell编程系列4--有类型变量:字符串.只读类型.整数.数组 有类型变量总结: declare命令和typeset命令两者等价 declare.typeset命令都是用来定义变量类型的 decla ...

  6. Java 部分排序算法

    . import java.io.*;import java.math.*;import java.util.*;public class Algr{ public static int array[ ...

  7. A Mysql backup script

    UseCentOS can help IT managers to get rid of the boring learning methods, quick grasp Linux technolo ...

  8. zabbix discovery

    preface(见面礼): 仅扫tcp端口: netstat -tnlp|egrep -i "$1"

  9. 算法(第四版)C# 习题题解——2.1

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  10. 希尔排序算法的php实现

    虽然现在各种程序语言都有其各自强大的排序库函数,但是这些底层实现也都是利用这些基础或高级的排序算法. 理解这些复杂的排序算法还是很有意思的,体会这些排序算法的精妙~ 一.希尔排序(shell sort ...

随机推荐

  1. Mac OS安装php-redis扩展

    下载php-redis(用于php5.x的版本),地址:https://nodeload.github.com/nicolasff/phpredis/zip/master. 如果是php7.2,选择p ...

  2. PHP匹配Email、URL、IP

    /* * 正则表达式匹配 */ $email = '137813369@qq.com'; $regex = '/\w+([−+.]\w+)*@\w+([−.]\w+)*\.\w+([−.]\w+)*/ ...

  3. gprof的使用介绍

    转于:http://blog.chinaunix.net/uid-25194149-id-3215487.html #不知道这是在哪里找的了,感谢各位~ 性能分析工具gprof介绍Ver:1.0 目录 ...

  4. Ubuntu sudo: add-apt-repository: command not found

    安装缺少的指令即可 $ sudo apt-get install software-properties-common python-software-properties

  5. jeecg中一对多的关系映射

    1.首先看一张前端页面: 有一张主表和3张子表,显示这3张子表的代码如下 <t:tabs id="tt" iframe="false" tabPositi ...

  6. 字符串的问题(substr,find用法)

    链接:https://www.nowcoder.com/acm/contest/77/C来源:牛客网 字符串的问题 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他 ...

  7. zookeeper实战:SingleWorker代码样例

    我们需要一个“单点worker”系统,此系统来确保系统中定时任务在分布式环境中,任意时刻只有一个实例处于活跃:比如,生产环境中,有6台机器支撑一个应用,但是一个应用中有30个定时任务,这些任务有些必须 ...

  8. PorterDuff.Mode

    参考:http://weishu.me/2015/09/23/Xfermode-in-android/ Sa = Source alphaDa = Dest alphaSc = Source colo ...

  9. NGUI的UIPanel、UIButton、AtlasMaker、Widget、Anchor、Tween、RectTransform

    全文请看:http://note.youdao.com/noteshare?id=f7b476be35ec554e311bc13ef60b62ef

  10. ApacheOFBiz的相关介绍以及使用总结(一)

    由于最近一段时间在给一个创业的公司做客户关系管理CRM系统,限于人力要求(其实是没有多少人力),只能看能否有稳定,开源的半成品进行改造,而且最好不需要前端(js)相关开发人员的支援就可以把事情做成,经 ...