Question

It will eventually be part of a larger script so it needs to be shell scripted. A simple task in other languages, but I'm having trouble accomplishing it in shell. Basically I have a string and I want to insert a "." at all possible indices within the string. The output can be on newlined or separated by spaces. Can anyone help?

Example:
input: "abcd"

output: ".abcd
a.bcd
ab.cd
abc.d
abcd."

OR

output: ".abcd a.bcd ab.cd abc.d abcd."

Was it helpful?

Solution

A simple for loop would do:

input=abcd
for ((i=0; i<${#input}+1; i++))
do
    echo ${input::$i}.${input:$i}
done

This just slices up the string at each index and inserts a .. You can change the echo to something else like appending to an array if you want to store them instead ouf output them, of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top