5

I've got a script that I'm reading the input from users. Here's my code:

if [ -z $volreadexists ]; then
        echo -e "\tThis will overwrite the entire volume (/dev/vg01/$myhost)...are you sure?"
        read REPLY
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            echo -e "\t\tContinuing"
            syncvolume
        else
            echo "Fine...skipping"
        fi
    fi

I'm having to use read REPLY because read by itself doesn't insert tabs. What I'm looking for is something similar to:

read -p "\tDoes this look OK? (n for No)" -n 1 -r

Where \t would tab over the read prompt.

How can I add tabs to a read prompt?

UPDATE: Thanks for the great answer from @gniourf!:

read -p $'\tDoes this look OK? (n for No)' -n 1 -r

However, I found an issue. When I attempt to use a variable there it doesn't translate it:

read -p $'\tThis will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? ' -n 1 -r

becomes

        This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure?

where I want:

        This will overwrite the entire volume (/dev/vg01/server1)...are you sure?

Using doublequotes doesn't work either :(

Any ideas?

user3063045
  • 2,019
  • 2
  • 22
  • 35

2 Answers2

4

Just use ANSI-C quoting:

read -p $'\tDoes this look OK? (n for No)' -n 1 -r

Now, if you want to use variable expansions too, you can mix different quotes like so:

read -p $'\t'"This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? " -n 1 -r

Here I only used ANSI-C quoting for the tab character. Make sure you don't leave any spaces between $'\t' and "This will....".

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
0

I ended up referencing this answer:

Read a variable in bash with a default value

and created a workaround. It's not perfect, but it works:

myhost="server1"
if [ -z $volreadexists ]; then
    read -e -i "$myhost" -p $'\tJust checking if it\'s OK to overwrite volume at /dev/vg01/'
    echo
    if [[ $REPLY =~ ^$myhost[Yy]$ ]]; then
        echo -e "\t\tContinuing"
    else
        echo "Fine...skipping"
    fi
fi
Community
  • 1
  • 1
user3063045
  • 2,019
  • 2
  • 22
  • 35