Skip to content

Instantly share code, notes, and snippets.

@jamband
Last active December 23, 2015 14:19
Show Gist options
  • Save jamband/6647582 to your computer and use it in GitHub Desktop.
Save jamband/6647582 to your computer and use it in GitHub Desktop.
bash スクリプトの配列操作
#!/bin/bash
declare -a a=(a1 a2 a3)
# 配列をすべて表示
echo "${a[@]}"
# a1 a2 a3
# 配列の先頭に要素を追加
a=(a0 ${a[@]})
echo "${a[@]}"
# a0 a1 a2 a3
# 配列の後尾に要素を追加
a=(${a[@]} a4)
echo "${a[@]}"
# a0 a1 a2 a3 a4
# 配列の先頭の要素を削除
a=(${a[@]:1})
echo "${a[@]}"
# a1 a2 a3 a4
# 配列の後尾の要素を削除
a=(${a[@]:0:$(expr ${#a[@]} - 1)})
echo "${a[@]}"
# a1 a2 a3
# 配列の先頭の要素を表示
echo "${a[0]}"
# a1
# 配列の後尾の要素を表示
echo "${a[$(expr ${#a[@]} - 1)]}"
# a3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment