Skip to content

Instantly share code, notes, and snippets.

@cpburnz
Last active August 29, 2015 14:02
Show Gist options
  • Save cpburnz/164eeaee47877efed51a to your computer and use it in GitHub Desktop.
Save cpburnz/164eeaee47877efed51a to your computer and use it in GitHub Desktop.
From a bash shell, store new-line separated strings into a variable, and execute a command for each line.
#!/bin/sh
#
# From a bash shell, store new-line separated strings into a variable, and
# execute a command for each line.
#
# Author: Caleb P. Burns
# License: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# Version: 1.0.0
# Enter new-line separated strings into $LINES.
read -rd '' LINES <<'EOF'
line 1
line 2
line 3
line 4
EOF
# Iterate over each line in $LINES, and do something with it.
# - NOTE: Do not double-quote $LINES; otherwise, it will be treated as one
# single line.
# - NOTE: Depending on the style of each line, you may want to double-quote
# $LINE. If $LINE is a single value with spaces, then you want to double
# quote it.
for LINE in $LINES; do echo "$LINE"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment