bash数组总结

int32位 posted @ Mar 22, 2016 03:55:52 PM in linux , 1686 阅读
转载请注明: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

카지노사이트 说:
2022年9月05日 11:03

I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here<a href="https://casinoseo.net/" target="_blank" title="카지노사이트">카지노사이트</a>

Bushra batool 说:
2023年9月22日 22:03

Composition: Composition is the arrangement of elements within a photograph. Photographers consider factors like framing, perspective, lines, balance, and lighting to create visually appealing and engaging images. christmas mini sessions

Bushra batool 说:
2023年9月23日 00:48

Some buildings, particularly commercial and public facilities, may have asbestos management plans in place to monitor and address asbestos-related issues over time. mold removal

Naveed 说:
2023年11月21日 15:30

Thanks for sharing us. 메이저놀이터

Naveed 说:
2023年11月23日 00:14

This was really an interesting topic and I kinda agree with what you have mentioned here! situs bokep

Naveed 说:
2023年11月24日 19:39

i love reading this article so beautiful!!great job! <a href="http://www.familyvictory.com/">kontol kuda</a>

Naveed 说:
2023年11月24日 19:39 I think that thanks for the valuabe information and insights you have so provided here. kontol kuda
richerson 说:
2023年12月24日 21:55

Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. SMLE

James 说:
2023年12月26日 13:49

This post is a testament to your expertise in the field. Your in-depth exploration of the topic, combined with practical examples, makes it a valuable resource. I'm constantly amazed by the caliber of content your blog consistently offers. Keep up the great work! best way to learn hebrew

Alexa 说:
2023年12月27日 16:12

Your writing style is captivating and makes complex topics easy to digest. This post is another example of your commitment to delivering insightful content. The thought and research you put into each article are evident, and it's something your readers truly appreciate. Keep up the fantastic work! английский с нуля

Bushra 说:
2024年1月12日 00:24 Great! It sounds good. Thanks for sharing.. fast vpn
SEO 说:
2024年1月12日 22:15

Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, translation services fort lauderdale

James 说:
2024年6月10日 13:52

This post is a prime example of the exceptional quality of content your blog consistently offers. Your ability to combine research, storytelling, and real-world applications is remarkable. Your dedication to enlightening your readers is evident in every word. Thank you for being a reliable source of knowledge and inspiration. laser ontharen transgender

토지노 说:
2024年7月05日 04:55

The term "토지노" could be a cultural or industry-specific term from Korea. It might be linked to entertainment, technology, or even a colloquial expression used in specific contexts. Understanding its exact meaning requires exploring its usage and implications in Korean culture. <a href="https://www.outlookindia.com/plugin-play/%ED%86%A0%EC%A7%80%EB%85%B8%EC%82%AC%EC%9D%B4%ED%8A%B8-%EC%B6%94%EC%B2%9C-%EA%B5%AD%EB%82%B4-%ED%86%A0%EC%A7%80%EB%85%B8-%EC%88%9C%EC%9C%84-%EB%AA%A9%EB%A1%9D-2024%EB%85%84">토지노</a>

토지노 说:
2024年7月05日 04:57

The term "토지노" could be a cultural or industry-specific term from Korea. It might be linked to entertainment, technology, or even a colloquial expression used in specific contexts. Understanding its exact meaning requires exploring its usage and implications in Korean culture. <a href="https://www.outlookindia.com/plugin-play/%ED%86%A0%EC%A7%80%EB%85%B8%EC%82%AC%EC%9D%B4%ED%8A%B8-%EC%B6%94%EC%B2%9C-%EA%B5%AD%EB%82%B4-%ED%86%A0%EC%A7%80%EB%85%B8-%EC%88%9C%EC%9C%84-%EB%AA%A9%EB%A1%9D-2024%EB%85%84">토지노</a>

rumaharis 说:
2024年8月24日 02:59

Rumahbola adalah situs bola parlay resmi dan situs slot gacor terpercaya di indonesia

Jason 说:
2024年10月19日 15:44

I really enjoyed this post. Your insights and experiences are incredibly valuable. Keep up the excellent work! international movers


登录 *


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