bash ãé åãæ±ãã
ã·ã§ã«ã¹ã¯ãªããã§é åãæ±ããã®ã§ é åãæ±ã£ã¦ã¿ãã
ä»åã¯ããµã³ãã«ã®ããã«ãå¤æ°åã大æåã«ãã¦ãã¾ãããã¡ããå°æåã使ãã¾ãã
大æåã¯ãããã«ç°å¢å¤æ°ãã¹ã¯ãªããéãããã»ã¹éã§å¤ã渡ãã¨ãã«ä½¿ããã¨ãå¤ãããã§ãã
é åã®å®ç¾©
é åã®å®ç¾©ã«ã¯åºæ¬çã«ï¼ã¤ããã¾ãã
bash ã®é åã®å®ç¾©ã®ä¾
declare ARR=() declare -a ARR # ã¾ã㯠arr=() #ç¡ç²¾ããå ´å
é åã®å®ç¾©ãæ¹è¡ããå ´åã¯ãæååã§ã¯ã©ã¼ããæã£åãæ©ãã
arr=("a b c ")
é åã®åé¤
å®ç¾©ããé åã¯åé¤ã§ãã¾ãã
unset -v ARR unset -v ARR[@] # ã¾ã㯠unset -v ARR[*] # ã¾ãã¯
é åã®åæå¤ãæå®ãã宣è¨
åæå¤ãæå®ãã宣è¨ããããã¨ã§æ確ã«ãªãã¾ãã
ARR=( a b c d e )
åºåãæåã¯ã空ç½ãæååã®ç¶ãããã®ã§ãã
ãã£ã¨è©³ç´°ã«ããã°ãåºåãæåã¯ç°å¢å¤æ° IFS
ã§æå®ããããã®ã«ãªãã¾ãã
é åã®å ¨è¦ç´ ãè¦ã
é åã®è¦ç´ ã®ä¸èº«ãå ¨é¨è¦ããã¨ãåºæ¥ã¾ãã
ARR=(a b c) echo ${ARR[@]} echo ${ARR[*]} # ã¾ãã¯
é åã®è¦ç´ æ°
é
åã®è¦ç´ æ°ã¯ ${#å¤æ°å}
ã使ããã¨ã§åå¾ãããã¨ãåºæ¥ãã
é åã®è¦ç´ æ°ã®åå¾ä¾
length=${#ARR[@]} length=${#ARR[*]} # ã¾ãã¯
@
㨠*
ã®éã
ARR[@]
㨠ARR[*]
ã¯ãã»ã¼åçã®æ©è½ãæã¤ããä¸ç¹ã ãç°ãªãã
ããã¯åºåãæåã@
㯠空ç½ã§åºåãã®ã«å¯¾ãã *
㯠IFS ã§åºåãã
@
㨠*
ã®éãã®ä¾
$ ARR=(a b c d e ) $ IFS=- $ echo "${ARR[@]}" a b c d e $ echo "${ARR[*]}" a-b-c-d-e
ãã ããããã«ã¯ã©ãã¼ã·ã§ã³ãã¤ããªãå ´åã¯[@]/[*]
ã«éããç¾ããªããããã¯IFSå¤æ°ãä½ç¨ããããã
ã¯ã©ãã¼ã·ã§ã³ãã¤ããªãå ´åã ã¨ãIFSã§æå®ããåºåãæåã§åºåããããã¨ã«ãIFSã§åºåã£ã¦æ¬¡ã«ã®ã³ãã³ãã«æ¸¡ãããã®ã§éãã¯ãªãã
ç°å¢å¤æ°ã®IFSã§åºåãæåãæå®ãããã¨ã§ãåºåã¨å ¥åã使ããã
# [*]ã«ããã«ã¯ã©ã¼ãã¼ã·ã§ã³æ¯è¼
$ IFS=- $ echo ${ARR[*]} a b c d e $ echo "${ARR[*]}" a-b-c-d-e
# ããã«ã¯ã©ã¼ãã¼ã·ã§ã³ãç¡ãã®ä¾ããã«
IFS=# ARR=(a b c ) echo ${ARR[*]} #=> a b c
ããã¯ãbashã§ã¯æ¬¡ã®ããã«ãé ã«è§£éããããã¨è¦ããããã¨ç解ããããã§ãã
IFS='#' ARR=(a b c ) echo ${ARR[*]} #=> a b c # 1. echo a${IFS}b${IFS}c # IFSä»ãã§å¤æ°å¦çãã # 2. echo a#b#c # IFSä»ã®æååã¨ãªã # 3. echo a b c #IFS=# ã§åºåãããå¼æ°ã«ãªããã³ã¬ãã³ãã³ãã«æ¸¡ãããã
é åã®è¦ç´ ãèµ°æ»ããã
é åã®æå®çªç®ã®å¤ãåãåºãã«ã¯ ã¤ã³ããã¯ã¹çªå·ã使ãã
é åã®è¦ç´ ãè¦ã¦ããä¾ã
$ ARR=(a b c ) $ echo ${ARR[0]} # => a $ echo ${ARR[1]} # => b $ echo ${ARR[2]} # => c $ echo ${ARR[3]} # ãªã«ããªãã®ã§ç©ºã£ã½ãåºã
é åã®é次å¦ç
é åã®è¦ç´ ã®ä¸èº«ãåãåºãã¦ãé çªã«è¦ã¦ããã«ã¯ã for ã«ã¼ãã使ãã®ã楽
for e in ${ARR[@]} ; do echo $e done
é åã®è¦ç´ ãåé¤ãã
é
åã®æå®çªç®ã®è¦ç´ ãåé¤ããã®ã¯ã å¤æ°ã®åé¤ã¨åãã unset
ã使ãã
åé¤ããé åã¯ãã¨ã®é åããã¤ã³ããã¯ã¹ãæ¬ æãããã®ã¨ãªãã
é åã®è¦ç´ ã®åé¤ã®ä¾
unset -v ARR[1]
åé¤å¾ã®é åã®æ§é
$ for idx in ${!ARR[@]} ; do echo " [$idx] => ${ARR[$idx]} " ; done [0] => a [2] => c
åé¤å¾ã®é åã®å度æ¡çªãã
Reã¤ã³ããã¯ã¹ãã¦ããªãã³ãã¼ããã«ã¯ãããä¸åº¦å®ç¾©ãç´ããæ©ãã¨æã
$ B=( ${ARR[@]} ) $ for idx in ${!B[@]} ; do echo " [$idx] => ${B[$idx]} " ; done [0] => a [1] => c
é åã®è¦ç´ ã®è¿½å ã
è¦ç´ ã®è¿½å ããå度é åãåçæããäºã«ãªãããã§ãããBashã«ã¯å°ç¨ã®æ¼ç®åãç¨æããã¦ãã¾ãã
å°ç¨æ¼ç®å +=()
ãã¤ã¡ã¼ã¸ããããã¦ããã¨æãã¾ãã
å°ç¨æ¼ç®å +=()
ã«ã¤ãã¦
ARR+=(c) # è¦ç´ ã®è¿½å
é åã®è¿½å ï¼åçæï¼ã®ä¾
$ ARR=(a b) $ ARR=(${ARR[@]} c) # æ«å°¾ã«è¿½å ãã $ echo ${BRR[@]} a b c
é åã®è¿½å ï¼ãªãã¬ã¼ã¿ï¼ã®ä¾
$ ARR=(a b) $ ARR+=(c) # 追å ãã $ echo ${ARR[@]} a b c
push / unshift
é åã« push / unshift ããããã«è¦ç´ ã®ã¤ã³ããã¯ã¹ãæå®ããã®ã¯ç¡çãªã®ã§ãé åã®åå®ç¾©ã使ã
## push ARR=(${ARR[@]} 'element') # æ«å°¾ã«è¿½å (push)ãã ## unshift ARR=('element' ${ARR[@]} ) # å é ã«è¿½å (unshift)ãã
é åã®å¤æ°ã¢ã¯ã»ã¹ ${ARR} 㨠$ARR
é
åã«ã{ }
ãã¤ããã«ã¢ã¯ã»ã¹ããå ´åãé
åã®å
é ã®è¦ç´ ãè¿ã£ã¦ãããå¤ãããã®äººã«ã¯ããªãã¿ãªã®ãããããªãããç§ã¯è¿·ãã®ã§ä½¿ããªããã¨ãã¦ãã
$ echo ${ARR[@]} # => a b c $ echo $ARR # => a
å人çã«ãã¾ã使ããªãã®ã§ãã©ãããå½¹ç®ãããã®ãç¥ããªãã
è¦ç´ ã®åãåºã ã¹ã©ã¤ã¹
é åãã¹ã©ã¤ã¹ãããããããªã®ãããããã¨ãªã®ã§è¦ç´ ã®ã¹ã©ã¤ã¹ããæ¹æ³ãç¥ã£ã¦ããããã
è¦ç´ ãã¹ã©ã¤ã¹ããã«ã¯ãå°ç¨ã®è¨æ³ {ARRAY[@]:X:n}
ã使ããX ã¯ã¤ã³ããã¯ã¹ n ã¯ã»ããåæ°
é åã®ã¹ã©ã¤ã¹ã®ä¾
$ ARR=(a1 a2 a3 a4 a5) $echo ${ARR[@]:0:2} #=> a1 a2
ã¡ãªã¿ã«ã-1
ãªã©ã®ããã°ã©ã ã§å®çªã®ã使ããã
é åã®æ大åæ°ãã大ããªåæ°ãæå®ãã¦ããé åã®æ大åæ°ã¾ã§ããåãåºããªãã
ã©ããã¦ãæ«å°¾ãã»ã»ããã¨ãã¯è¨ç®ãã ${ARR[@]:0:((${#ARR[@]}-1))}
ãã®ããã«åãåºãã
é åãæ«å°¾ããã¹ã©ã¤ã¹:追è¨
bash 4 ããã®æ©è½ã§ãé åãæ«å°¾ããåç §ãããã¨ãåºæ¥ãããã«ãªã£ã¦ãã¾ãã
${ARR[@]: -1} # ' -1 ' ã¹ãã¼ã¹ãã¤ã㦠-1 ãæ¸ã
é åãã¹ã©ã¤ã¹ãã¦å é ã ããæ¶ã
å é ãæ¨ã¦ã¦shift çãªãã¨ãåºæ¥ã¾ãã
ARR=("${ARR[@]:1}")
ã¹ã©ã¤ã¹ãå¿ç¨ããã°ããããªãã¨ãåºæ¥ã¾ãã
é åã®é·ã
é
åã®é·ã㯠$#ARR[@]
ã§ã¢ã¯ã»ã¹ãã
ARR=(this is a test of array) echo ${#ARR[@]}
æï¼ã»ã³ãã³ã¹ï¼words)ãèªåå±éãã¦é åã«
é
åã®ããã©ã«ãå®ç¾© ARRAY=()
ã¯åèªãåºåãã®ã«ä½¿ãããããã¨ãã¨è±èªã¯ã¹ãã¼ã¹ã§åãã¦æ¸ãã®ã§ãããããã®ã
$ARR=( Hello I am takuya ) $ IFS=, $ echo "${ARR[*]}" Hello,I,am,takuya
ã³ãã³ãã®åºåçµæããæ´å½¢ãããåºåãªã©ã¯ãããããã«é åã«å±éã§ãã¾ãã
IFSã使ã㨠é åã® join ( çµå ) ãã§ããã
${my_arr[@]}
ã ã¨åºåããã¹ãã¼ã¹ã«ãªããã *
ã使ã㨠IFSåºåã
ã使ãã®ã§ãIFSãä¸æã«ä½¿ãã°ãé
åã®çµå(join) ããããã¨ãã§ããã
çµåãé¢æ°ã«ããä¾
my_arr=( aaa bbb ccc ) ( IFS=, ; printf %s "${a[*]}") ;
ãããé¢æ°ã«ãã¦ã
function join_arr()( IFS=,; echo "$*" )
ãªãã¦ãã¨ãã§ããã
function join_arr()( IFS=,; echo "$*" ) join_arr abc xyz 123 # --> abc,xyz,123
IFSãç¥ãã°ãé
åã®å®ç¾© a=( aaa bbb )
ãããµãã·ã§ã«ã¨IFSã®åºåãæåã ã¨æ°ã¥ãã®ã§é¢ç½ãã
ã«ã¼ãã¨ä½µãã¦ä½¿ããfor ç·¨
é åã¯ã«ã¼ãã¨ä½µãã¦ä½¿ããç¹°ãè¿ãå¦çãããããã«æãã¾ãã
ãã®ãããã«ã¼ãã¨ä½µãã¦ä½¿ãã®ãã»ã¨ãã©ã ã¨æãã¾ããfor ã«ã¼ããç¥ã£ã¦ããã°ãååæ¦ããã¨æãã¾ãã
for ã¨ä½µãã¦ä½¿ãä¾ ( for .. in
for e in ${ARR[@]} ; do echo $e ; done
for ã¨ä½µãã¦ä½¿ãä¾ ( for index in
for idx in ${!ARR[@]} ; do echo ${ARR[$idx]} ; done
for ã¨ä½µãã¦ä½¿ãä¾ ( Cè¨èªã¹ã¿ã¤ã«
for (( idx=0; idx< ${#ARR[@]}; idx++ )) ; do echo ${ARR[$idx]} ; done
while ã¯ï¼
while ã¯ãfor ã«ã¼ãã¨ç価交æå¯è½ãªã®ã§ãç¹ã«ç´°ãã説æå¿ è¦ã¯ãªããã¨æãã¾ãã
ãã ãé åãããã ãåãç¡éã«ã¼ãã¯ãã使ããããªã®ã§å°ãä¾ãä¸ãã¦ããããã¨æãã¾ãã
ARR=(a b c) while (( ${#ARR[@]} > 0 )) ; do echo $ARR ARR=("${ARR[@]:1}") done
ããã¯ãé
åãå
é ããä¸ã¤ãã¤åãåºãã¦ãå
é ã®è¦ç´ ã$ARR
ã§åç
§ãã¦ãå
é ã®è¦ç´ ãã¹ã©ã¤ã¹åé¤ãã¦ãã¾ãã
ã¹ã¯ãªãããé ã®ãªãã§å±éãã¦ç解ããã«ã¯ãforã«ã¼ãã¨ãã¦ã¯whileã§ããã ãåãã«ã¼ãã®ã»ããã·ã³ãã«ã§ç解ããããããããã¾ããã
ããï¼shift ã¯ï¼ä½ç½®å¤æ°ã¯ï¼
shift 㯠ä½ç½®å¤æ°é åï¼ã³ãã³ãã©ã¤ã³å¼æ°ã»é¢æ°ã®å¼æ°ï¼ã®ã¨ãã«ä½¿ããã®ã§ãä¸è¬çãªé åã ããæ±ããã®è¨äºã«ã¯ç»å ´ããã¾ãããããã§ã ããããunshift é¢æ°ãç¨æããã¦ããªãã
é£æ³é å
bash ã®é åã§ã¯ é£æ³é åï¼ããã·ã¥ã»dict ) ãæ±ããã¨ãåºæ¥ã¾ããããã¯ã¾ãå¥è¨äºã«ã
ãã®ä»
xargs ã使ã£ã¦ãã¤ãã§æ¸ãæãããã¨ã¯åºæ¥ã¾ããã
2016-12-27 追è¨
bash ã® ã¹ã©ã¤ã¹ã®è¨è¿°ãééã£ã¦ãã¾ãããä¿®æ£ãã¾ããã
a=(a b c) echo ${a[@]: -1} #=> c
åºæ¥ãªãã¨æ¸ãã¾ããããåºæ¥ã¾ãããç§ã®è¨è¿°æ¹æ³ã誤ãã§ããã
${a[@]:-1} # åããªã ${a[@]: -1} # åã
ã¹ãã¼ã¹ãå¿ è¦ã§ããã
åèè³æ
2021-02-16
IFS ãæ¤ç´¢ãããªãã®ã§ããã¼ã¯ã¼ããæ示çã«æå®ããã
IFS ã®åºåãæåã¤ãã£ããé åã®çµå ( join ) ã«ã¤ãã¦è¨åã