在bash中,我怎么能打印列表的前n个元素? [英] In bash, how can I print the first n elements of a list?

查看:124
本文介绍了在bash中,我怎么能打印列表的前n个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

庆典,我怎么能打印列表的第一个 N 元素?

例如,在第一10个文件在此列表

  FILES = $(LS)

更新:我忘了说,我想打印在一行中的元素,当您打印整个列表,就像回声$文件


解决方案

  FILES =(*)
回声$ {FILES [@]:0:10}

应能正常工作,即使有文件名中的空格。

FILES = $(LS)创建一个字符串变量。 FILES =(*)创建一个数组。查看在bash 使用数组本页面了解更多例子。 (感谢lhunath)

In bash, how can I print the first n elements of a list?

For example, the first 10 files in this list:

FILES=$(ls)

UPDATE: I forgot to say that I want to print the elements on one line, just like when you print the whole list with echo $FILES.

解决方案

FILES=(*)
echo "${FILES[@]:0:10}"

Should work correctly even if there are spaces in filenames.

FILES=$(ls) creates a string variable. FILES=(*) creates an array. See this page for more examples on using arrays in bash. (thanks lhunath)

这篇关于在bash中,我怎么能打印列表的前n个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆