Skip to content

Instantly share code, notes, and snippets.

@jalex19100
Created June 24, 2021 21:58
Show Gist options
  • Save jalex19100/a9e4e7bd15a46aad872971d259fba562 to your computer and use it in GitHub Desktop.
Save jalex19100/a9e4e7bd15a46aad872971d259fba562 to your computer and use it in GitHub Desktop.
Split on Delimiter in bash
# 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