0

I would like to create a code to obtain the following result:

echo -e "\t\tPlease enter your name?"
read name
name="${name,,}"
for i in $(echo "Hello " $name) ; do
    echo ${i^} 
done | sed 's/^/\t\t/;s/$/, here is your order:/'

If I enter the name like: mike denver

The output is:

<here come out the right space of two tabs>Hello, here is your order:
<here come out the right space of two tabs>Mike, here is your order:
<here come out the right space of two tabs>Denver, here is your order:

Basically, it create the right space from the left side based on the number of tabs that I put (\t), but it repeats the print line for Hello plus for each names entered. I didn't understand how to put sed command outside. My expected result should be this:

<here come out the right space of two tabs>Hello, Mike Denver here is your order: 
  • Rather than using a loop, just use the variable directly? echo "Hello, ${name^}" | sed ... ? – icarus Mar 19 '20 at 22:47
0

Rather than using the for loop, it's much easier to just do this:

echo -e "\t\tPlease enter your name?"
read -a name
name="${name}"
echo -e "\t\tHello ${name[@]^}, here is your order:"

Output:

            Please enter your name?
mike denver
            Hello Mike Denver, here is your order:

Double quotes around the argument for echo will expand the variable and print everything together at once in the desired format.

0

You could use an array for the name and printf:

read -p $'\t\tPlease enter your name?\n' -a name
name=( "${name[@],,}" )
printf '\t\tHello %s, here is your order:\n' "${name[*]^}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.