Assuming there are no :
s in the array data, use bash
pattern substitution to squeeze the ::
to :
while assigning the string to $array
, then show the whole array, then just element #2:
a="name1::1.1.1.1::ps -ax"
IFS=: array=(${a//::/:}) ; echo ${array[@]} ; echo "${array[2]}"
Output:
name1 1.1.1.1 ps -ax
ps -ax
But what if there are :
s in the array data? Specifically in the third field, (the command), and only in that field. Use read
with dummy variables to absorb the extra ::
separators:
a="name1::1.1.1.1::parallel echo ::: 1 2 3 ::: a b"
IFS=: read x a y b z <<< "$a"; array=("$x" "$y" "$z"); printf "%s\n" "${array[@]}"
Output:
name1
1.1.1.1
parallel echo ::: 1 2 3 ::: a b