I have two type ranges which I need to get it in a single variable. How can I do that?

eg: I have one range like bs0401 to bs0405 (bs0401,bs0402,bs0403...) and another range like bn0201 to bn0205(bn0201,bn0202,bn0203..) and I need both these expansion under one variable.

I can do for single sequence like

h=$(seq -f "bs%02g" 0401 1 0405) 

but not sure how can Include multiple sequence assigned in a single variable so that echo $h will given like

  • Does is have to be in a vairable? You can do it with a for loop like this: for i in $(seq -f "bs%02g" 0401 1 0405) $(seq -f "bn%02g" 0201 1 0205); do echo $i; done. I do not need to mention, that you can do anything else instead of echo, right? – nobody Jun 21 '20 at 16:09
  • If you assign $h as you write, echo will output values in one line and not each line by line. At least on my computer. – nobody Jun 21 '20 at 16:13
  • 1
    Or even h=$(echo bn{0401..0405} bs{0201..0205}) ; echo $h | tr " " "\n". Also to get the 4 digits it's "bs%04g" – bu5hman Jun 21 '20 at 16:45

You can include several commands inside a command substitution ($(...)). So in your case

h=$(seq -f 'bs%04g' 0401 1 0405; seq -f 'bn%04g' 0201 1 0205)

gives you

$ echo $h
bs0401 bs0402 bs0403 bs0404 bs0405 bn0201 bn0202 bn0203 bn0204 bn0205
  • This is a great answer, but I'm curious why you didn't upvote the question. – user1717828 Jun 21 '20 at 21:00

If you wanted to store multiple values in a variable, you'd need a shell with array support like csh, ksh, rc, zsh, bash, fish, yash.

In zsh, bash, yash -o braceexpand you could do it without relying on a seq command here with:

list=(bs{0401..0405} bn{0201..0205})

Or in ksh93:

list=(bs{401..405%04d} bn{201..205%04d})

Though in this particular case, you could shorten it to:


Which would work in all 4 shells.


string=$(seq -f bs%04g 401 405)

You're actually storing into $string one value which is the output of the seq command with the trailing newline characters removed.

You could store the concatenated output of two seq commands with:

string=$(seq -f bs%04g 401 405; seq -f bn%04g 201 205)


string="$(seq -f bs%04g 401 405)
$(seq -f bn%04g 201 205)"

Here adding back a newline character between them to compensate for the one removed by the first command substitution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.