User Tools

Site Tools


programming-languages:bash:array

Array

An array in bash can only be one dimension.

Return array from function

#!/bin/bash
 
create_array() {
    # read a file into an array
 
    local arr=()
 
    while read -r line; do
        arr+=($line)
    done < "/path/file.ext"
 
    echo "${arr[@]}"
}
 
for i in $(create_array); do
    echo $i
done
#!/bin/bash
 
name="$(pwd)/my_file.txt"
 
text=("a aa" 
    "b bb" 
    "c cc")
 
printf "%s\n" "${text[@]}" > $name  # rewrite file if exists
cat $name
programming-languages/bash/array.txt · Last modified: 2023/11/06 10:24 by tormec