shell 函数参数为数组传递
2015-11-21 19:30
435 查看
You cannot pass an array, you can only pass its elements (i.e. the expanded array).
#! /bin/bash function f() { a=("$@") ((last_idx=${#a[@]} - 1)) b=${a[last_idx]} unset a[last_idx] for i in "${a[@]}" ; do echo "$i" done echo "b: $b" } x=("one two" "LAST") b='even more' f "${x[@]}" "$b" echo =============== f "${x[*]}" "$b"The other possibility would be to pass the array by name:
#! /bin/bash function f() { name=$1[@] b=$2 a=("${!name}") for i in "${a[@]}" ; do echo "$i" done echo "b: $b" } x=("one two" "LAST") b='even more' f x "$b"
http://stackoverflow.com/questions/16461656/bash-how-to-pass-array-as-an-argument-to-a-function
相关文章推荐
- bash基本语法
- line-ending
- .bash_profile不生效解决办法
- shell 脚本
- bash: rz: command not found
- shell 脚本判断某个文件是否存在
- Xshell连接很慢的解决办法
- spark-shell --conf
- Linux Shell脚本编程--wc命令
- shell脚本中if判断的一些参数及含义
- shell-bash学习04读取输入、分隔符、流程控制
- shell编程之一基础
- Xshell连接虚拟机的最简单方式
- Citrix Xen300上海培训之我见
- 我从其他Shell脚本中学到了什么?
- linux shell编程 5 判断登陆用户,然后重启关闭系统
- linux shell 编程4 判断当前的是不是目录、 传递参数的个数
- Git版本库理解和Git bash 命令行
- linux 命令及shell 知识小点汇集
- shell之nginx安装+虚拟主机