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}
2
  • Note that echo {0-9A-F} works in zsh with the BRACE_CCL option. Commented Feb 20, 2017 at 16:26
  • Note that in base 10 you could also say {00..99} to get the same output. Commented Jan 9, 2020 at 21:16

3 Answers 3

24

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
1
  • 1
    This is a nicer trick than manually write each digit in 0-9A-F! Commented Feb 19, 2017 at 20:40
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.

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.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.