This is a small script to convert binary number to decimal numbers.  We already shown how to do hex2dec conversion in our the previous post.  In this post too we will use (()) and bc commands to accomplish our task.

Below we will see number of ways to do this conversion using .

Method1: Use (()) brace expatiation.  Let me explain below code

#!/bin/bash
read -p “Please enter a BINARY number: ” BIN1
echo “The decimal value of $HEX1 is $((2#$BIN1))”

Save above file as bin2dec.sh

Change permissions to this script now and execute as follows

chmod +x bin2dec.sh

Executing shell script

./bin2dec.sh

Method2: Using bc command

#!/bin/bash
read -p “Please enter BIN number: ” BIN1

echo “ibase=8; $BIN1” | bc

Save above file as bin2dec.sh

Change permissions to this script now and execute as follows

chmod +x bin2dec.sh

Executing shell script./bin2dec.sh

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.