Last active
July 10, 2021 01:09
-
-
Save mckern/5b4dfcf006f0cbd00ff07737c1b8418a to your computer and use it in GitHub Desktop.
This file contains 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
this | |
is | |
an | |
example | |
of | |
a | |
newline | |
delimited | |
file |
This file contains 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
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 $ |
This file contains 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
#!/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