Loren Zimmer
Loren Zimmer

Reputation: 480

Parse hex values into string for shell script

I'm trying to put together what should be a fairly simple shell script. The script will have a parameter that is a decimal value. The script will need to convert the decimal to a hex value and then combine that value with a command to be run.

For example if I run the command

./myscript.sh 45

It would need to convert 45 to hex (2d) and then append 2d to the end of a string.

Upvotes: 0

Views: 904

Answers (1)

edd
edd

Reputation: 1417

You have the function print that might be useful.

printf "%x" 45
2d

It works similarly to high level programming languages print function/call where it can transform a value using % formatting. Here, %x, formats the first given positional argument to its hex value.

Then you could utilize this by assigning the value to a variable and take it from there.

x=$(printf "%x" 45)

Also, the first argument to a script lives in $1.

Upvotes: 2

Related Questions