8

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}
  • Note that echo {0-9A-F} works in zsh with the BRACE_CCL option. – TonioElGringo Feb 20 '17 at 16:26
18

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

Using printf:

$ printf '%02x ' {0..255}

The format string %02x says to format the output as a zero-filled, two-digit, lower-case, hexadecimal number.

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

Bash only understands base 10 integer ranges or alphabetical character ranges 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.

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.