Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
18
Help us understand the problem. What is going on with this article?
@YumaInaura

シェル — スペースが含まれる配列要素をループする

More than 1 year has passed since last update.

注意

コメント欄参照

検証

ループの中で、添え字を使って配列要素にアクセスする。

example.sh
arr=(
"Space delimited text A"
"Space delimited text B"
"Space delimited text C"
)


for ((i = 0; i < ${#arr[@]}; i++))
do
  echo "${arr[$i]}"
done

結果

$ bash example.sh
Space delimited text A
Space delimited text B
Space delimited text C

問題

以下の書き方では、スペースの区切りで別要素として扱われてしまっていた。

example.sh
arr=(
"Space delimited text A"
"Space delimited text B"
"Space delimited text C"
)

for item in ${arr[@]}; do
  echo "$item"
done
$ bash example.sh
Space
delimited
text
A
Space
delimited
text
B
Space
delimited
text
C

環境

  • bash 4.4.0
18
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
YumaInaura
Ruby on Rails 業務経験 約5年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible AWS など / いなうらゆうま / YumaInaura / 稲浦悠馬

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
18
Help us understand the problem. What is going on with this article?