Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple bash CSV array issue

Tags:

arrays

bash

csv

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.

like image 419
Atomiklan Avatar asked Dec 29 '25 03:12

Atomiklan


1 Answers

Set IFS to , and use the read command with a here-string

IFS=, read -r -a arr <<<"20,80,443,53"
printf "%s\n" "${arr[@]}"

20
80
443
53
like image 188
iruvar Avatar answered Dec 30 '25 23:12

iruvar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!