LoginSignup
0
0

More than 5 years have passed since last update.

Bash Brace Expansion で入れ子を用いて、0 から 20 までの連番値を 2 桁化(ゼロパディング)

Last updated at Posted at 2018-05-10

こんにちは。
Bash Brace Expansion (ブレース展開)の入れ子を用いて、0 から 20 までの連番値を zero-padding して 2 桁化させてみました。

$ echo {0{0..9},{10..20}}
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
$ echo {00..20}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

また 3 桁化の例は(だんだんスマートではなくなりますが)、

$ echo {0{0{0..9},{10..99}},{100..110}}
000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110

seq コマンド

単純な用途には seq コマンドで足りるようです。

$ echo $(seq -s '.txt ' -w 0 11)
00.txt 01.txt 02.txt 03.txt 04.txt 05.txt 06.txt 07.txt 08.txt 09.txt 10.txt 11.txt
$

bash 4

bash 4.x の Brace Expansion は zero-padding してくれます。homebrew を使って bash 4.x をインストールし確かめました。

インストール前:

$ which -a bash
/bin/bash
$ echo x.{01..11}
x.1 x.2 x.3 x.4 x.5 x.6 x.7 x.8 x.9 x.10 x.11

インストール後:

$ brew info bash
bash: stable 4.4.23 (bottled), HEAD
Bourne-Again SHell, a UNIX command interpreter
https://www.gnu.org/software/bash/
$ brew install bash
$ which -a bash
/usr/local/bin/bash
/bin/bash
$
$ bash
$ echo x.{01..11}
x.01 x.02 x.03 x.04 x.05 x.06 x.07 x.08 x.09 x.10 x.11
$ exit
$
$ echo x.{01..11}
x.1 x.2 x.3 x.4 x.5 x.6 x.7 x.8 x.9 x.10 x.11
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0