IMLC.ME
Search…
en-US
Powered By GitBook
How to split string into array in Bash
A snippet to split string into an array. And shows you how to retrieve elements from the array.
1
set -x
2
3
STR="/path/to/a/config/file"
4
TMP=$IFS
5
IFS='/'
6
WORDS=($STR)
7
8
for i in "${WORDS[@]}"; do
9
echo "$i"
10
done
11
12
echo "#1 ${WORDS[1]}"
13
echo "#2 ${WORDS[2]}"
14
echo "#3 ${WORDS[3]}"
15
echo "#4 ${WORDS[4]}"
16
echo "#5 ${WORDS[5]}"
17
18
echo "#Last item ${WORDS[@]: -1}"
19
20
IFS=$TMP
Copied!
Last modified 1yr ago
Copy link