括弧は文字列ではなく配列を区切るために使用されるため、次のようになります。
ids="1 2 3 4";echo ${ids// /|}
1|2|3|4
いくつかのサンプル:$ids
2つの文字列を入力: a b
およびc d
ids=("a b" "c d")
echo ${ids[*]// /|}
a|b c|d
IFS='|';echo "${ids[*]}";IFS=$' \t\n'
a b|c d
... そして最後に:
IFS='|';echo "${ids[*]// /|}";IFS=$' \t\n'
a|b|c|d
配列がアセンブルされ、の最初の文字で区切られていますが、配列の各要素で$IFS
スペースが置き換えられています。|
あなたがするとき:
id="${ids[@]}"
文字列ビルドを、スペースによる配列 のマージから文字列ids
型の新しい変数に転送します。
注:スペースで区切られた文字列を"${ids[@]}"
指定すると(アットマークの代わりに星を使用)、の最初の文字で区切られた文字列がレンダリングされます。"${ids[*]}"
*
@
$IFS
何をman bash
言うか:
man -Len -Pcol\ -b bash | sed -ne '/^ *IFS /{N;N;p;q}'
IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read
builtin command. The default value is ``<space><tab><newline>''.
で遊ぶ$IFS
:
declare -p IFS
declare -- IFS="
"
printf "%q\n" "$IFS"
$' \t\n'
文字通り、、space
およびtabulation
(または)を意味しline-feed
ます。つまり、最初の文字はスペースです。の使用は*
と同じようになり@
ます。
しかし:
{
IFS=: read -a array < <(echo root:x:0:0:root:/root:/bin/bash)
echo 1 "${array[@]}"
echo 2 "${array[*]}"
OIFS="$IFS" IFS=:
echo 3 "${array[@]}"
echo 4 "${array[*]}"
IFS="$OIFS"
}
1 root x 0 0 root /root /bin/bash
2 root x 0 0 root /root /bin/bash
3 root x 0 0 root /root /bin/bash
4 root:x:0:0:root:/root:/bin/bash
注:この行は、永続的に設定されることなく、区切り文字としてIFS=: read -a array < <(...)
使用されます。これは、出力行にスペースが区切り文字として表示されるためです。:
$IFS
#2