Skip to content

Instantly share code, notes, and snippets.

@mckern
Last active July 10, 2021 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckern/5b4dfcf006f0cbd00ff07737c1b8418a to your computer and use it in GitHub Desktop.
Save mckern/5b4dfcf006f0cbd00ff07737c1b8418a to your computer and use it in GitHub Desktop.
this
is
an
example
of
a
newline
delimited
file
ryan@bender fun-with-arrays.RHRjym $ bash reading-files-as-arrays.sh example-lines.txt
this is the first example's length: 46
this is the first example's first item:
this
is
an
example
of
a
newline
delimited
file
this is the second example's length: 4
this is the second example's first item:
this
ryan@bender fun-with-arrays.RHRjym $
#!/usr/bin/env bash
# first, we read the file using stdin
example1=$(< "${1}")
# then, we read the file using `read`, assigning it to the array variable
# "example2", and using raw input
read -r -a example2 < "${1}"
echo -e "this is the first example's length: ${#example1}"
echo -e "this is the first example's first item:\n${example1[0]}"
echo -e "\n\n"
echo -e "this is the second example's length: ${#example2}"
echo -e "this is the second example's first item:\n${example2[0]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment