bash数组总结

int32位 posted @ Mar 22, 2016 03:55:52 PM in linux , 1304 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

 

bash支持两种数组,一种是索引数组,一种是关联数组

索引数组

数组的值类型是任意的,索引也未必一定要连续,当做列表理解更好

下面总结下索引数组,即列表:

1. 声明

declare -a a

2. 初始化

a=(1 2 3 4)
# OR
a=([0]=2 [3]=8) # 注意长度为2,不是4

3. 获取列表长度

size=${a[@]}

4. 追加元素

a+=(11 12 13 14)

5. 赋值

a[1]=9

6. 按索引读取

value=${a[0]} # 读取索引0的值

7. 删除某元素

unset a[0]

8. 清空数组

unset a

9. 切片

echo ${a[@]:1:3} # 从索引1开始的3个元素
# 也支持负数索引,-1表示最后一个,但负数必须用括号括住
last=${a[@]:(-1):1}

10. 遍历

for i in ${a[@]}
do
    echo $i
done

关联数组

1. 声明

declare -A map

2. 初始化

map[key1]=value1
map[key2]=value2
# or
map=([key1]=value1 [key2]=value2)

3. 长度

size=${!map[@]}

4. 获取键集合

keyset=${!map[@]}

5. 获取值集合

values=${map[@]}

6. 遍历

for key in ${!map[$@]}
do
    echo $key:${map[$key]}
done

 

转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
Liwovosa 说:
2021年4月19日 19:15

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts. brawl stars apk

Top SEO 说:
2021年5月01日 16:59

This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! basketball legends unblocked

Top SEO 说:
2021年5月01日 17:17

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. shopping

Top SEO 说:
2021年5月01日 17:18

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. ラブドール

seo 说:
2021年7月06日 18:08

Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work! 123 movies

seo 说:
2021年7月06日 18:11

New site is solid. A debt of gratitude is in order for the colossal exertion. 123movies

seo 说:
2021年7月06日 18:13

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. 123 movies

seo 说:
2021年7月08日 13:08

Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot. sa gaming

seo 说:
2021年7月10日 19:46

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details. superslot

seo 说:
2021年7月10日 19:55

it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. pg slot

seo 说:
2021年7月13日 15:11

Very nice article. I enjoyed reading your post. very nice share. I want to twit this to my followers. Thanks !. sa gaming

seo 说:
2021年7月13日 17:58

Admiring the time and effort you put into your blog and detailed information you offer!.. 2021 waec expo

seo 说:
2021年7月13日 23:23

I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. joker gaming

seo 说:
2021年7月14日 15:29

Every online casino has its distinct customer's guide and service provider. It is for the purpose of guiding and servicing their customer's to what they need in their gambling activities. We all know that once a player gamble online, he is also about to know what is the basic of computer and the internet in his gaming needs. 카지노사이트

seo 说:
2021年8月08日 14:45

We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. xe tải 2 tấn

seo 说:
2021年9月01日 10:18 Owning a passport may be the last thing on your mind. I know, who needs a passport? Well, you just might if you ever get the travel bug and want to go somewhere beyond our national borders. You may not need it now, but since an adult passport lasts for 10 years, applying early may be the smartest thing you ever did. buy passport online
seo 说:
2021年9月01日 10:39

When you look at statistics, you will find that obesity is currently sweeping the globe at an alarming rate. Over 1 billion adults are classed as overweight while 300 million are classed as obese according to the World Health Organization. It's a scary figure that needs to be changed, but that is not going to happen overnight. welche Nüsse zum Abnehmen

seo 说:
2021年9月14日 13:10

I am an incurable optimist. I believe that with faith, hope, hard work, perseverance and persistent, we can achieve all our desired goals and aspirations in life. I believe that to everything there is a season, and a time to every purpose under the sun, and when the time and season comes we must make the best use of it. I believe that we must put in our best in everything and leave the rest to God. I believe that life is not all that bed of roses because if life were all one unbroken symphony of success, success would itself become... UBA magic banking

seo 说:
2021年9月15日 13:12

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea. אשרת קנדה באינטרנט

seo 说:
2021年9月16日 19:57

Instagram is fast becoming a popular social media tool. With million of users worldwide it's no wonder more and more businesses are using the tool as part of their marketing mix. Small businesses have also realised that it can be used as a cost effective means to build brand awareness, increase sales, engage with customers and further maximise brand recognition. instant followers for instagram

seo 说:
2021年9月18日 23:35

If you have experienced a scenario where in you logged on to your real money account in an online casino to find out that all your money or bankroll was emptied, then you probably have landed on the seemingly right but actually a wrong place to play online casino games. Incident like this one is so unfortunate of course. But, aside from it is unfortunate, it also reveals a serious problem that most of the online casino players fall onto. pokie place

seo 说:
2021年9月22日 00:03

SEO techniques are evolving every passing day and therefore the need to select a SEO Services company that has high quality services is very important. A good SEO services company should be able to provide you with a lot of options and will be aware of the latest SEO techniques that are in place. Get to know about the traits that you need to look out for while selecting a SEO services company for your business. jasa seo indonesia


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter