从列表/变量中删除项目 - Bash

时间:2016-03-30 05:54:42

标签: linux bash

如果我将变量称为文件列表:

files = $(ls * .txt)

然后我要删除列表中的第一项(文件)

由于

克里夫

1 个答案:

答案 0 :(得分:2)

你永远不应该解析ls的输出;见http://mywiki.wooledge.org/ParsingLs

幸运的是,在您的情况下,您实际上并未使用ls的任何功能; Bash已处理*.txt部分,因此ls非常多余。

你可以这样写:

# Set files to an array of the files with names ending in '*.txt':
files=(*.txt)

# Set files to an array consisting of ${files[1]}, ${files[2]}, ...:
files=("${files[@]:1}")

(请参阅the Bash Reference Manual, § 3.5.3 "Shell Parameter Expansion";搜索${parameter:offset}。)