Question
In a Bash script I would like to split a line into pieces and put them into an array. For example:
- Paris, France, Europe
How-To
Try this command:
- IFS=', ' read -a array <<< "$string"
- echo "${array[0]}"
- for element in ${array[@]}
- do
- echo "$element"
- done
- for index in ${!array[@]}
- do
- echo "$index ${array[index]}"
- done
- echo "${#array[@]}"
- echo "${array[-1]}"
- #!/bin/bash
- PWD=`pwd`
- echo "Current PWD=$PWD"
- IFS='/' read -a array <<< "$PWD"
- echo "There are ${#array[@]} element(s)."
- for index in ${!array[@]}
- do
- echo "$index ${array[index]}"
- done
Supplement
* Blog - Separate string into array from bash
沒有留言:
張貼留言