Skip to main content
deleted 3 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Bash (or any other shell, really) is not a good tool for general programming. Bash doesn't even handle floating point arithmetic, let alone anything more complex. The shell is a shell, you can write simple little scripts in one, but you shouldn't think of it as a general purpose programming language: it isn't.

I'm afraid that if you insist on using bash for something like this, you will be forced to use more and more complex workarounds, starting with printf:

$ printf '%x\n' $"$((0xa+1))"
b

The good news is that you can at least also do this:

$ printf '%x\n' 11
b

So you can easily convert from one to the other, which means you don't need to call printf every time:

var=0xa

let $((var++))
echo "Var (decimal): $var"
printf 'Var (hex): %x\n' "$var"

Running the above would print:

$ foo.sh
Var (decimal): 11
Var (hex): b

In other words, you can do your thing in hex and not care about how it is displayed until you need to print something out.

But no, you won't find a native way of doing this sort of thing in the shell because you are trying to use the shell for a purpose it was not designed for.

Bash (or any other shell, really) is not a good tool for general programming. Bash doesn't even handle floating point arithmetic, let alone anything more complex. The shell is a shell, you can write simple little scripts in one, but you shouldn't think of it as a general purpose programming language: it isn't.

I'm afraid that if you insist on using bash for something like this, you will be forced to use more and more complex workarounds, starting with printf:

$ printf '%x\n' $((0xa+1))
b

The good news is that you can at least also do this:

$ printf '%x\n' 11
b

So you can easily convert from one to the other, which means you don't need to call printf every time:

var=0xa

let $((var++))
echo "Var (decimal): $var"
printf 'Var (hex): %x\n' "$var"

Running the above would print:

$ foo.sh
Var (decimal): 11
Var (hex): b

In other words, you can do your thing in hex and not care about how it is displayed until you need to print something out.

But no, you won't find a native way of doing this sort of thing in the shell because you are trying to use the shell for a purpose it was not designed for.

Bash (or any other shell, really) is not a good tool for general programming. Bash doesn't even handle floating point arithmetic, let alone anything more complex. The shell is a shell, you can write simple little scripts in one, but you shouldn't think of it as a general purpose programming language: it isn't.

I'm afraid that if you insist on using bash for something like this, you will be forced to use more and more complex workarounds, starting with printf:

$ printf '%x\n' "$((0xa+1))"
b

The good news is that you can at least also do this:

$ printf '%x\n' 11
b

So you can easily convert from one to the other, which means you don't need to call printf every time:

var=0xa

((var++))
echo "Var (decimal): $var"
printf 'Var (hex): %x\n' "$var"

Running the above would print:

$ foo.sh
Var (decimal): 11
Var (hex): b

In other words, you can do your thing in hex and not care about how it is displayed until you need to print something out.

But no, you won't find a native way of doing this sort of thing in the shell because you are trying to use the shell for a purpose it was not designed for.

Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Bash (or any other shell, really) is not a good tool for general programming. Bash doesn't even handle floating point arithmetic, let alone anything more complex. The shell is a shell, you can write simple little scripts in one, but you shouldn't think of it as a general purpose programming language: it isn't.

I'm afraid that if you insist on using bash for something like this, you will be forced to use more and more complex workarounds, starting with printf:

$ printf '%x\n' $((0xa+1))
b

The good news is that you can at least also do this:

$ printf '%x\n' 11
b

So you can easily convert from one to the other, which means you don't need to call printf every time:

var=0xa

let $((var++))
echo "Var (decimal): $var"
printf 'Var (hex): %x\n' "$var"

Running the above would print:

$ foo.sh
Var (decimal): 11
Var (hex): b

In other words, you can do your thing in hex and not care about how it is displayed until you need to print something out.

But no, you won't find a native way of doing this sort of thing in the shell because you are trying to use the shell for a purpose it was not designed for.