Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux variable expansion problem ?

Tags:

linux

shell

I'd like to get a sequence of number like this 01,02,..,30 But if I use {01..30}, I will get 1,2,..30, the zero before single-digit number is missing. How can I get the format like 01,02..30 ? Thanks in advance

like image 606
zjffdu Avatar asked Aug 21 '26 12:08

zjffdu


2 Answers

In Bash4, your {01..30} brace expansion actually works as you want it to. If you are on Bash3 or lower you can use the printf builtin along with the brace expansion to get what you want without resorting to external commands.

$ printf "%02d " {1..20}
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
like image 141
SiegeX Avatar answered Aug 24 '26 04:08

SiegeX



seq -w 1 30  # ought to work

as in:


for number in $(seq -w 1 30); do
 touch /tmp/bleah$number.dat
done
like image 20
jcomeau_ictx Avatar answered Aug 24 '26 03:08

jcomeau_ictx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!