Senger CodeLab 🚀

Hexadecimal To Decimal in Shell Script

September 29, 2026

📂 Categories: Bash
Hexadecimal To Decimal in Shell Script

Navigating different number systems is a fundamental skill in computing, especially when dealing with low-level data, memory addresses, or network protocols. One common task for developers and system administrators is converting values from hexadecimal to decimal. While many programming languages offer built-in functions for this, performing Hexadecimal To Decimal in Shell Script provides a powerful and flexible way to automate tasks directly within your Linux or Unix environment. This guide will delve into various methods, from simple command-line tools to more intricate script logic, ensuring you can confidently handle such conversions. Understanding these techniques can significantly enhance your ability to parse logs, configure systems, and debug applications efficiently without relying on external compilers or complex programs.

Understanding Hexadecimal and Decimal Number Systems

Before diving into the conversion process, it’s crucial to grasp the basics of both hexadecimal and decimal number systems. The decimal system (base-10) is what we use daily, utilizing ten unique digits (0-9). Each position in a decimal number represents a power of 10. For instance, the number 123 is (1 10^2) + (2 10^1) + (3 10^0).

Hexadecimal (base-16), on the other hand, uses sixteen unique symbols: 0-9 and A-F. Here, A represents 10, B is 11, and so on, up to F which is 15. Each position in a hexadecimal number represents a power of 16. This system is widely used in computing because it offers a more compact representation of binary data, as four binary digits can be represented by a single hexadecimal digit. For example, 0xFF is equivalent to (15 16^1) + (15 16^0) in decimal, which equals 255. Mastering this fundamental concept is the first step in effective base conversion in Linux environments.

Often, you’ll encounter hexadecimal values in contexts like MAC addresses, color codes (e.g., FF0000), or memory dumps. Being able to quickly convert these values to their decimal equivalents using bash hexadecimal conversion techniques is invaluable for tasks such as calculating network ranges or manipulating raw data. Shell scripts provide a direct and efficient avenue for this, integrating seamlessly into existing automation workflows.

Methods for Hexadecimal To Decimal in Shell Script

Converting hexadecimal to decimal in a shell script can be achieved using several robust methods, each suitable for different scenarios. The most common and often recommended approach leverages Bash’s arithmetic expansion capabilities, which inherently understand different number bases. By prefixing a hexadecimal number with 0x, Bash automatically interprets it as a base-16 value within an arithmetic context. This simplifies the scripting hexadecimal values process significantly for numerical operations.

For example, to convert 0xFF, you can simply use echo $((0xFF)). Bash will perform the conversion and output 255. This method is incredibly efficient for direct conversions of known hex values. Another powerful tool for advanced shell scripting techniques is the printf command, which can format output and perform base conversions. The format specifier %d is used for decimal output, and %x for hexadecimal input, making it a versatile option for command line hex to dec conversions.

To convert a hexadecimal string to a decimal integer in a shell script, the most straightforward method involves using Bash’s arithmetic expansion by prefixing the hexadecimal value with 0x. For instance, echo $((0xABC)) will directly output the decimal equivalent. Alternatively, the printf command with %d for decimal output and %x for input can also perform this conversion reliably, as in printf "%d\n" 0xABC. These methods are efficient and built into most modern Unix-like environments.

Beyond these built-in shell features, external utilities like bc (arbitrary precision calculator) or awk can also be employed, particularly for more complex calculations or when dealing with numbers larger than what typical shell arithmetic might handle easily. Each approach offers its own advantages in terms of simplicity, performance, and flexibility, making shell script number systems conversion a versatile aspect of scripting.

Using Bash’s Arithmetic Expansion

Bash’s arithmetic expansion is a highly favored method due to its simplicity and direct integration into the shell. When you enclose an expression in $(( )), Bash evaluates it as an arithmetic expression. By default, numbers starting with 0x are treated as hexadecimal. This makes it incredibly easy to perform a Hexadecimal To Decimal in Shell Script operation.

!/bin/bash Define a hexadecimal value HEX_VALUE="0xFF" Convert using arithmetic expansion DEC_VALUE=$((HEX_VALUE)) echo "The hexadecimal value $HEX_VALUE is $DEC_VALUE in decimal." Example with a different hex value HEX_IP_PART="0xAC" Equivalent to 172 DEC_IP_PART=$((HEX_IP_PART)) echo "Part of an IP address (hex $HEX_IP_PART) is $DEC_IP_PART in decimal." 

This method is clean and requires minimal code, making it ideal for quick conversions within a script. It’s important to ensure your hexadecimal string is properly prefixed with 0x for Bash to interpret it correctly. This elegant solution highlights why Bash is such a powerful tool for system-level tasks and data manipulation.

Leveraging the printf Command

The printf command is another powerful utility for number system conversions, offering more control over output formatting. While its primary role is formatted printing, it can also interpret numbers in different bases. To convert hexadecimal to decimal, you can instruct printf to read a hexadecimal number and output it as a decimal. This method is especially useful when dealing with input that might not always be prefixed with 0x, although typically it’s best practice to ensure consistent input formats.

!/bin/bash Define a hexadecimal value HEX_INPUT="A5" Convert using printf, assuming 0x prefix for interpretation DEC_OUTPUT=$(printf "%d\n" "0x$HEX_INPUT") echo "The hexadecimal value $HEX_INPUT is $DEC_OUTPUT in decimal." Another example directly with 0x prefix HEX_COLOR="0x3C" DEC_COLOR=$(printf "%d\n" "$HEX_COLOR") echo "A color component (hex $HEX_COLOR) is $DEC_COLOR in decimal." 

The printf hexadecimal method provides a robust alternative, particularly if you need to integrate the conversion into more complex string formatting operations. It’s a fundamental utility for any shell Question & Answer :

Can someone help me to convert a hexadecimal number to decimal number in a shell script?

E.g., I want to convert the hexadecimal number bfca3000 to decimal using a shell script. I basically want the difference of two hexadecimal numbers.

My code is:

var3=`echo "ibase=16; $var1" | bc` var4=`echo "ibase=16; $var2" | bc` var5=$(($var4-$var3)) # [Line 48] 

When executing, I get this error:

Line 48: -: syntax error: operand expected (error token is "-") 

To convert from hex to decimal, there are many ways to do it in the shell or with an external program.

With bash:

$ echo $((16#FF)) 255 

with bc:

$ echo "ibase=16; FF" | bc 255 

with perl:

$ perl -le 'print hex("FF");' 255 

with printf:

$ printf "%d\n" 0xFF 255 

with python:

$ python -c 'print(int("FF", 16))' 255 

with ruby:

$ ruby -e 'p "FF".to_i(16)' 255 

with node.js:

$ node -e "console.log(parseInt('FF', 16))" 255 

with rhino:

$ rhino -e "print(parseInt('FF', 16))" 255 

with groovy:

$ groovy -e 'println Integer.parseInt("FF",16)' 255