I'm trying to do something fairly simple, and I'm just coming up short. Here is an example variable I'm trying to work with:
20,80,443,53
The variable is just a string of ports separated by commas. I need to get those into an array.
Here is one way:
#!/bin/bash
v="20,80,443,53"
IFS=, a=($v) # Split
echo ${a[0]} # Display
echo ${a[1]}
echo ${a[2]}
echo ${a[3]}
Thanks to gniourf_gniourf for pointing out that IFS was modified as the result of the assignment. Here is my quirky work around. Now I see why others did things differently.
v="20,80,443,53"
PREV_IFS="$IFS" # Save previous IFS
IFS=, a=($v)
IFS="$PREV_IFS" # Restore IFS
echo ${a[0]}
echo ${a[1]}
echo ${a[2]}
echo ${a[3]}
IFS variable! will bite you in the back if you forget about it.Ports=('20','80','443','53');
for e in "${lic4l[@]}"; do
echo $e
I hope this will help and print the ports in the given variable.