Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

84%
+9 −0
Q&A How to read lines into an array in Bash

Your code is correct. You have declared your variable as an array, and you are successfully appending to it. To display all of the elements of your variable, try echo "${my_array[@]}". (Another an...

posted 4y ago by r~~‭  ·  edited 4y ago by r~~‭

Answer
#4: Post edited by user avatar r~~‭ · 2021-03-22T20:22:05Z (almost 4 years ago)
  • Your code is correct; another answer suggests switching to `declare -p my_array`, but this is not necessary or useful (all it does is print the attributes of the variable, which implicitly means that `my_array` defaults to being a normal string variable—the same effect is achieved by omitting the `declare` altogether). You have declared your variable as an array, and you are successfully appending to it.
  • To display all of the elements of your variable, try `echo "${my_array[@]}"`.
  • To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.
  • To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).
  • `echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.
  • ---
  • Note also that in Bash versions >= 4, the `readarray` builtin will do all of this work for you:
  • `readarray -t my_array < my_file.txt`
  • You don't even need to declare `my_array` beforehand if you use this builtin. Bash 4 was released in 2009, so unless you're working on a pretty old or stripped-down system, you are probably safe using that.
  • Your code is correct. You have declared your variable as an array, and you are successfully appending to it.
  • To display all of the elements of your variable, try `echo "${my_array[@]}"`. (Another answer suggests `declare -p` for this, but `declare -p` will possibly give you more information than you wanted. Also, be aware that `declare -p` isn't intended to *substitute* for your `declare -a`; it's an additional command to show the status of your variable.)
  • To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.
  • To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).
  • `echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.
  • ---
  • Note also that in Bash versions >= 4, the `readarray` builtin will do all of this work for you:
  • `readarray -t my_array < my_file.txt`
  • You don't even need to declare `my_array` beforehand if you use this builtin. Bash 4 was released in 2009, so unless you're working on a pretty old or stripped-down system, you are probably safe using that.
#3: Post edited by user avatar r~~‭ · 2021-03-22T20:17:59Z (almost 4 years ago)
  • Your code is correct; another answer suggests switching to `declare -p my_array`, but this is not necessary or useful (all it does is print the attributes of the variable, which implicitly means that `my_array` defaults to being a normal string variable—the same effect is achieved by omitting the `declare` altogether). You have declared your variable as an array, and you are successfully appending to it.
  • To display all of the elements of your variable, try `echo "${my_array[@]}"`.
  • To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.
  • To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).
  • `echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.
  • Your code is correct; another answer suggests switching to `declare -p my_array`, but this is not necessary or useful (all it does is print the attributes of the variable, which implicitly means that `my_array` defaults to being a normal string variable—the same effect is achieved by omitting the `declare` altogether). You have declared your variable as an array, and you are successfully appending to it.
  • To display all of the elements of your variable, try `echo "${my_array[@]}"`.
  • To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.
  • To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).
  • `echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.
  • ---
  • Note also that in Bash versions >= 4, the `readarray` builtin will do all of this work for you:
  • `readarray -t my_array < my_file.txt`
  • You don't even need to declare `my_array` beforehand if you use this builtin. Bash 4 was released in 2009, so unless you're working on a pretty old or stripped-down system, you are probably safe using that.
#2: Post edited by user avatar r~~‭ · 2021-03-22T20:10:20Z (almost 4 years ago)
  • Your code is correct; another answer suggests switching to `declare -p my_array`, but this is not necessary or useful (all it does is print the attributes of the variable, which implicitly means that `my_array` defaults to being a normal string variable—the same effect is achieved by omitting the `declare` altogether). You have declared your variable as an array type, and you are successfully appending to it.
  • To display all of the elements of your variable, try `echo "${my_array[@]}"`.
  • To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.
  • To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).
  • `echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.
  • Your code is correct; another answer suggests switching to `declare -p my_array`, but this is not necessary or useful (all it does is print the attributes of the variable, which implicitly means that `my_array` defaults to being a normal string variable—the same effect is achieved by omitting the `declare` altogether). You have declared your variable as an array, and you are successfully appending to it.
  • To display all of the elements of your variable, try `echo "${my_array[@]}"`.
  • To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.
  • To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).
  • `echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.
#1: Initial revision by user avatar r~~‭ · 2021-03-22T20:07:28Z (almost 4 years ago)
Your code is correct; another answer suggests switching to `declare -p my_array`, but this is not necessary or useful (all it does is print the attributes of the variable, which implicitly means that `my_array` defaults to being a normal string variable—the same effect is achieved by omitting the `declare` altogether). You have declared your variable as an array type, and you are successfully appending to it.

To display all of the elements of your variable, try `echo "${my_array[@]}"`.

To display the current number of elements in your variable, try `echo "${#my_array[@]}"`.

To display the third element in the array, try `echo "${my_array[2]}"` (arrays in Bash are zero-indexed).

`echo $my_array` will only display the first element of the array, for what I presume are historical reasons, but that's the documented behavior. I would avoid using this ‘feature’ with array variables; it only leads to confusion.