「Bash」IFSで配列の要素をjoinするサンプル

2021年12月3日

書式
declare -a 配列名=(“study" “skill" “in","arkgame")
$(IFS=","; echo “${配列名[*]}")
IFSは区切り文字の環境変数です。bashの場合、空白文字が区切り文字として初期設定されています。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
declare -a cftLst=("study" "skill" "in","arkgame")
cft=$IFS
result="$(IFS=","; echo "${cftLst[*]}")"
IFS=$cft
echo ${result}
#!/bin/bash declare -a cftLst=("study" "skill" "in","arkgame") cft=$IFS result="$(IFS=","; echo "${cftLst[*]}")" IFS=$cft echo ${result}
#!/bin/bash
 
declare -a cftLst=("study" "skill" "in","arkgame")
cft=$IFS
result="$(IFS=","; echo "${cftLst[*]}")"
IFS=$cft
echo ${result}

実行結果
# sh test11.sh
study,skill,in,arkgame

batch

Posted by arkgame