Question about armstrong numbers

when i was having problems with trying to do arithmetic expansion and then adding it to a variable i decided to see how others had solved this problem.

i seen an unusual expression that i haven’t seen before in others answers. what i would like is more info on it, where i can look it up, what it’s called…

i’ll put mine here –

An=$1
total=0

for ((x=0; x<"${#An}"; x++)); do
    total=$(( "$total" + "${An:$x:1}" ** "${#1}" ))
done

it’s what is getting added to the total in braces, the An.... part

thank you

${An:$x:1} is a Shell Parameter Expansion. Particularly it is the ${parameter:offset:length} form that gets you a substring.

The offset is zero-based

$ x=hello
$ echo ${x:3:2}
lo

bash provides various parameter expansions that in other languages might be:

  • length
  • substring
  • trimleft & trimright
  • sub & gsub
  • “if x (is|is not) null then …”
    • set x to y
    • return y
  • toupper & tolower
  • and more

thank you for answering

when i look at ‘man bash’ AND your ‘hello’ explanation then what the man page was saying makes more sense. i have seen this in some of the basic python that i have played with.

cool, thank you

take care
em