Expanding a bash array only gives the first element

后端 未结 1 1135
温柔的废话
温柔的废话 2020-12-02 01:34

I want to put the files of the current directory in an array and echo each file with this script:

#!/bin/bash

files=(*)

for file in $files
do
    echo $fil         


        
相关标签:
1条回答
  • 2020-12-02 02:18

    $files expands to the first element of the array. Try echo $files, it will only print the first element of the array. The for loop prints only one element for the same reason.

    To expand to all elements of the array you need to write as ${files[@]}.

    The correct way to iterate over elements of a Bash array:

    for file in "${files[@]}"
    
    0 讨论(0)
提交回复
热议问题