12

I would like to know if there is a way of using bash expansion to view all possibilities of combination for a number of digits in hexadecimal. I can expand in binaries

In base 2:

echo {0..1}{0..1}{0..1}

Which gives back:

000 001 010 011 100 101 110 111

In base 10:

echo {0..9}{0..9}

Which gives back:

00  01 02...99

But in hexadecimal:

echo {0..F}

Just repeat:

{0..F}
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
David Borges
  • 155
  • 1
  • 9

3 Answers3

23

You can; you just need to break the range {0..F} into two separate ranges {0..9} and {A..F}:

$ printf '%s\n' {{0..9},{A..F}}{{0..9},{A..F}}
00
01
...
FE
EF
chepner
  • 7,341
  • 1
  • 26
  • 27
15

Using printf:

$ printf '%.2x\n' {0..255}

The format string %.2x says to format the output as a zero-filled, two-digit, lower-case, hexadecimal number (%02x would have done the same).

If you want upper-case, use %.2X.

Bash only understands base 10 integer ranges or ranges between ASCII characters in brace expansions of intervals.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
7

It's possible but it isn't nice:

echo {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}

As far as I can tell bash has no notion of hex ranges.

Satō Katsura
  • 13,138
  • 2
  • 31
  • 48