将文件名加载到不带目录名称或扩展名的数组中



我正在尝试将文件名加载到bash中的数组中。目前,我正在使用:

testarr=(csv/*.csv)

这给了我这样的元素

csv/filename.csv

如何仅将"文件名"添加到数组中?

谢谢!

最简单的方法是首先添加整个名称,然后使用参数扩展去除您不需要的部分:

testarr=( csv/*.csv )            # load literal filenames
testarr=( "${testarr[@]##*/}" )  # strip off directory names
testarr=( "${testarr[@]%.csv}" ) # strip off extensions

这也可以使用基本名称 -a 来完成:

testarr=($(basename -a csv/*.csv))

最新更新