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

后端 未结 8 1401
死守一世寂寞
死守一世寂寞 2021-02-19 20:00

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

For example, the first 10 files in this list:

FILES=$(ls)
8条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 20:34

    to do it interactively:

    set $FILES && eval eval echo \\\${1..10}

    to run it as a script, create foo.sh with contents

    N=$1; shift; eval eval echo \\\${1..$N}

    and run it as

    bash foo.sh 10 $FILES

提交回复
热议问题