How can I store the “find” command results as an array in Bash

后端 未结 8 1862
刺人心
刺人心 2020-11-22 16:29

I am trying to save the result from find as arrays. Here is my code:

#!/bin/bash

echo \"input : \"
read input

echo \"searching file with this          


        
8条回答
  •  星月不相逢
    2020-11-22 16:55

    If you are using bash 4 or later, you can replace your use of find with

    shopt -s globstar nullglob
    array=( **/*"$input"* )
    

    The ** pattern enabled by globstar matches 0 or more directories, allowing the pattern to match to an arbitrary depth in the current directory. Without the nullglob option, the pattern (after parameter expansion) is treated literally, so with no matches you would have an array with a single string rather than an empty array.

    Add the dotglob option to the first line as well if you want to traverse hidden directories (like .ssh) and match hidden files (like .bashrc) as well.

提交回复
热议问题