nsa
nsa

Reputation: 565

How to pass multiple files with variable filenames to the paste command

Usually when we wish to concatenate several files column wise and the filenames of the files are just consecutive increasing integers we can do the following:

#Imagine I have 10 files
paste {1..10} > out

However, I'm currently working on a script in which the ranges are variables, so I want to be able to do something like this

first=1
last=10
paste {"${first}".."${last}"} > out

This doesn't work as variables can't be correctly expanded within the curly braces. Is there an alternative syntax I can use to achieve the same result?

Upvotes: 0

Views: 329

Answers (2)

root
root

Reputation: 6038

If you don't want to use eval, you can use seq(1):

seq -s ' ' "$first" "$last"

Like so:

paste $(seq -s ' ' "$first" "$last") > out

Upvotes: 2

Ivan
Ivan

Reputation: 7267

Once upon a time a needed a seq like function but a way faster, so i made this

# Create sequence like {0..X}
cnt () { printf -v N %$1s; N=(${N// / 1}); printf "${!N[*]}"; }

$ cnt 5
0 1 2 3 4

And if we modify it a bit

# Create sequence like {X..Y}
cnt () { printf -v N %$2s; N=(${N// / 1}); N=(${!N[@]}); printf "${N[*]:$1} ${#N[@]}"; }

$ cnt 7 11
7 8 9 10 11

Upvotes: 1

Related Questions