How do you store a list of directories into an array in Bash (and then print them out)?

后端 未结 4 784
别那么骄傲
别那么骄傲 2020-12-05 09:52

I want to write a shell script to show a list of directories entered by a user and then for a user to select one of the directories with an index number based on how many di

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 10:30

    Update: my answer is wrong

    Leaving it here to address a common misunderstanding, below the line is erroneous.


    To put the directories in an array you can do...

    array=( $( ls -1p | grep / | sed 's/^\(.*\)/"\1"/') )
    

    This will capture the dir names, including those with spaces.


    Extracting from comments:

    literal quotes don't have any effect on string-splitting, so array=( echo '"hello world" "goodbye world"' ) is an array with four elements, not two

    @Charles Duffy

    Charles also supplied the following link Bash FAQ #50 which is an extended discussion on this issue.

    I should also draw attention to the link posted by @Dennis Williamson - why I shouldn't have used ls

提交回复
热议问题