<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Calculating Mathematical Expressions in Shell Scripting &#8211; Part 5	</title>
	<atom:link href="https://www.tecmint.com/mathematical-operations-in-shell-scripting/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.tecmint.com/mathematical-operations-in-shell-scripting/</link>
	<description>Tecmint - Linux Howtos, Tutorials, Guides, News, Tips and Tricks.</description>
	<lastBuildDate>Mon, 19 May 2025 04:54:45 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: Ravi Saive		</title>
		<link>https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-2308376</link>

		<dc:creator><![CDATA[Ravi Saive]]></dc:creator>
		<pubDate>Mon, 19 May 2025 04:54:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.tecmint.com/?p=4245#comment-2308376</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-2307908&quot;&gt;Geoff&lt;/a&gt;.

@Geoff,

Thanks for pointing this out! 

You’re absolutely right, the leading zero causes the shell arithmetic &lt;code&gt;$((…))&lt;/code&gt; to interpret the input as octal, which breaks the binary-to-decimal conversion for numbers like &lt;code&gt;01101&lt;/code&gt;.

A simple fix would be to strip leading zeros before performing the calculation. For example, you can add a line like this right after reading the input:
&lt;pre&gt;
Binary=$(echo &quot;$Binary&quot; &#124; sed &#039;s/^0*//&#039;)
&lt;/pre&gt;
This removes all leading zeros and ensures &lt;code&gt;$((…))&lt;/code&gt; treats the number correctly as decimal digits of a binary number.

Alternatively, you could update the input validation to disallow leading zeros (except for the number 0 itself), depending on your preference.

Thanks again for bringing this up — it’s a useful enhancement for anyone using the script today!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a target="_blank" href="https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-2307908">Geoff</a>.</p>
<p>@Geoff,</p>
<p>Thanks for pointing this out! </p>
<p>You’re absolutely right, the leading zero causes the shell arithmetic <code>$((…))</code> to interpret the input as octal, which breaks the binary-to-decimal conversion for numbers like <code>01101</code>.</p>
<p>A simple fix would be to strip leading zeros before performing the calculation. For example, you can add a line like this right after reading the input:</p>
<pre>
Binary=$(echo "$Binary" | sed 's/^0*//')
</pre>
<p>This removes all leading zeros and ensures <code>$((…))</code> treats the number correctly as decimal digits of a binary number.</p>
<p>Alternatively, you could update the input validation to disallow leading zeros (except for the number 0 itself), depending on your preference.</p>
<p>Thanks again for bringing this up — it’s a useful enhancement for anyone using the script today!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Geoff		</title>
		<link>https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-2307908</link>

		<dc:creator><![CDATA[Geoff]]></dc:creator>
		<pubDate>Sat, 17 May 2025 09:22:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.tecmint.com/?p=4245#comment-2307908</guid>

					<description><![CDATA[I realise this post is 9 years old, but it was recently featured on Linux Today (May 16, 2025) and so will have a new audience, who should be made aware that the binary-to-decimal conversion script has a bug. Observe:

Enter a binary number:
1101
Decimal Value: 13

Enter a binary number:
01101
Decimal Value: 41

The reason the leading zero throws it off is because $((...)) treats integers that have a leading zero as octal (for consistency with C constants). The script should either disallow leading zero when it checks the input, or strip off leading zeros before doing any calculations.]]></description>
			<content:encoded><![CDATA[<p>I realise this post is 9 years old, but it was recently featured on Linux Today (May 16, 2025) and so will have a new audience, who should be made aware that the binary-to-decimal conversion script has a bug. Observe:</p>
<p>Enter a binary number:<br />
1101<br />
Decimal Value: 13</p>
<p>Enter a binary number:<br />
01101<br />
Decimal Value: 41</p>
<p>The reason the leading zero throws it off is because $((&#8230;)) treats integers that have a leading zero as octal (for consistency with C constants). The script should either disallow leading zero when it checks the input, or strip off leading zeros before doing any calculations.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ravi Saive		</title>
		<link>https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-741024</link>

		<dc:creator><![CDATA[Ravi Saive]]></dc:creator>
		<pubDate>Mon, 18 Jan 2016 09:33:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.tecmint.com/?p=4245#comment-741024</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-740230&quot;&gt;Ian&lt;/a&gt;.

@Ian,

Can you point us the typo? so that we can correct it..]]></description>
			<content:encoded><![CDATA[<p>In reply to <a target="_blank" href="https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-740230">Ian</a>.</p>
<p>@Ian,</p>
<p>Can you point us the typo? so that we can correct it..</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ian		</title>
		<link>https://www.tecmint.com/mathematical-operations-in-shell-scripting/comment-page-1/#comment-740230</link>

		<dc:creator><![CDATA[Ian]]></dc:creator>
		<pubDate>Sat, 16 Jan 2016 16:52:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.tecmint.com/?p=4245#comment-740230</guid>

					<description><![CDATA[You got a typo in script2]]></description>
			<content:encoded><![CDATA[<p>You got a typo in script2</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
