0

I am writing a script that removes vowels from a string while preserving sequential white spaces, here is my code:

#!/bin/bash

result=""
for (( i = 0; i < ${#1}; i++ )); do
  [[ "${1:$i:1}" =~ [^aeiouAEIOU] ]] && result+="${1:$i:1}"
done
echo $result

It executes properly for example:

[user@localhost ~]$ bash remove_vowels.sh "Hi it's me"
H t's m #vowels removed successfully

But when my argument contains multiple white spaces:

[user@localhost ~]$ bash remove_vowels.sh "Hi it's     me"
H t's m #Whitespaces are gone

What do I need to change so my code work as desired?

1 Answers1

0

Double quote your variable to prevent the shell from applying word splitting on its contents:

echo "$result"

Search for "word splitting" in man bash for the details.

choroba
  • 231,213
  • 25
  • 204
  • 289