您可以使用循环来读取文件的每一行并将其放入数组
- # Read the file in parameter and fill the array named "array"
- getArray() {
- array=() # Create array
- while IFS= read -r line # Read a line
- do
- array+=("$line") # Append line to the array
- done < "$1"
- }
- getArray "file.txt"
如何使用你的数组:
- # Print the file (print each element of the array)
- getArray "file.txt"
- for e in "${array[@]}"
- do
- echo "$e"
- done