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:

  • Note that echo {0-9A-F} works in zsh with the BRACE_CCL option. Feb 20 '17 at 16:26
  • Note that in base 10 you could also say {00..99} to get the same output.
    – Ruslan
    Jan 9 '20 at 21:16

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}}
  • This is a nicer trick than manually write each digit in 0-9A-F!
    – dhag
    Feb 19 '17 at 20:40

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.


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.


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.