Created
June 24, 2021 21:58
-
-
Save jalex19100/a9e4e7bd15a46aad872971d259fba562 to your computer and use it in GitHub Desktop.
Split on Delimiter in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash | |
#!/usr/bin/env bash | |
IN="example@example;struff@stuff" | |
# Do something with each element separated by ; | |
IFS=';' read -ra ADDR <<< "$IN" | |
for i in "${ADDR[@]}"; do | |
# process "$i" | |
done | |
# Process whole content of $IN, each time one line of input separated by ; | |
while IFS=';' read -ra ADDR; do | |
for i in "${ADDR[@]}"; do | |
# process "$i" | |
done | |
done <<< "$IN" | |
# Possible without while, using HERE doc | |
read -r -d '' -a addr <<< "$in" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment