Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:29:31+00:00 2026-05-31T21:29:31+00:00

This statements works fine in BASH v. 4.1.7(1), but not in BASH v. 4.2.20(1)

  • 0

This statements works fine in BASH v. 4.1.7(1), but not in BASH v. 4.2.20(1)

num=${number:$counter:1}

I am getting “Bad Substitution” as the error. Any insights on why this is happening?
The objective is to do something similar to ${string:position:length} for getting sub-string.

Any help will be much appreciated. Thanks.

The original code which had this line is added below (line 15)

#!/bin/bash

echo "Please enter a number: "
read number

counter=0
answer=0
end=$(( ${#number} - 1 ))

echo -n "The sum of all digits of "
echo -n $number
echo -n " is "

while [ $counter -lt ${#number} ] ; do
num=${number:$counter:1}
if [ $counter -lt $end ] ; then
echo -n $num
echo -n "+"
else
echo -n $num
echo -n "="
fi
answer=$(( $answer + $num ))
counter=`expr $counter + 1`
done

echo $answer
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:29:32+00:00Added an answer on May 31, 2026 at 9:29 pm

    the expression is fine with 4.2.20 when i tested it. maybe you have something in your env?

    [localhost ~]$ bash --version
    GNU bash, version 4.2.20(1)-release (i386-redhat-linux-gnu)
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    
    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    [localhost ~]$ number=123
    [localhost ~]$ counter=0
    [localhost ~]$ echo $number
    123
    [localhost ~]$ num=${number:$counter:1}
    [localhost ~]$ echo $num
    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this error in ASP.NET application (whereas it works fine in Oracle) PLS-00703::multiple
I'm using this statement in SQLServer and it works fine: SELECT TOP 1000 *
Many times I see statements like following Y.CustomApp.superclass.render.apply(this, arguments); I know how apply works.
Can an if statement have more than one then statements? # this works a
In a view I can do this and the link works fine: <%= link_to
I've long used assignment inside long if statements to simplify things. But in this
I add C# group too in this, because this is not VB problem but
How can I put this two statements into one singel statement. They work something
I have problems running a dynamic LIKE statement in my project: this query works
i saw this In MySQL, joins work for INSERT, UPDATE, and DELETE statements. It's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.