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
I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried: declare -a my_array=() while read line; do my_array+=( "$line"...
#5: Post edited
How to read lines into an array in Bash
- I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried:
- ```
- declare -a my_array=()
- while read line; do
- my_array+=( "$line" )
- done < my_file.txt
- ```
But it seems to only add the first line.
- I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried:
- ```
- declare -a my_array=()
- while read line; do
- my_array+=( "$line" )
- done < my_file.txt
- ```
- But it seems to only add the first line as tested with `echo "$my_array"`.
#3: Post edited
- I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried:
`declare -a my_array=()``- while read line; do
- my_array+=( "$line" )
done < my_file.txt`- But it seems to only add the first line.
- I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried:
- ```
- declare -a my_array=()
- while read line; do
- my_array+=( "$line" )
- done < my_file.txt
- ```
- But it seems to only add the first line.
#2: Post edited
- I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried:
`declare -a my_array=()- while read line; do
- my_array+=( "$line" )
done < my_file.txt`- But it seems to only add the first line.
- I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried:
- `declare -a my_array=()`
- `
- while read line; do
- my_array+=( "$line" )
- done < my_file.txt`
- But it seems to only add the first line.
#1: Initial revision
How to read lines into an array in Bash
I wish to fill an array with strings, using Bash scripting. The strings are in a file, one per line. Here is what I've tried: ` declare -a my_array=() while read line; do my_array+=( "$line" ) done < my_file.txt ` But it seems to only add the first line.