Ryan Cauldwell
Ryan Cauldwell

Reputation: 61

bash script needs to read user input and use it as the argument for another command

So I've been searching the internet for an answer to this, and I can't seem to find anything. I have a bash script that I'm using at the moment, and one of the things that I'd like to do is prompt the user of the script to input a sentence saying what it is that they've used the script for, which then gets passed to CVS. If you don't know what CVS is, it's just a script control function, and the cvs commit that I'm using takes a string and appends it to the files' being committed.

The basics of what I'v tried is as follows:

read -p "Please enter what you did here: "
echo
cvs commit -m '$READ' filename.txt

The problem is that when I enter a string with multiple words, that read interprets that as multiple arguments being passed, and is storing each word as an individual variable, which is breaking my cvs function.

So basically, how do I prompt for a string input and use that input as one variable?

Thanks :)

Upvotes: 0

Views: 207

Answers (3)

David C. Rankin
David C. Rankin

Reputation: 84521

You have not specified a variable to hold what the user enters, so by default your input ends up in the variable REPLY. Per man bash/read:

If no names are supplied, the line read is assigned to the variable REPLY.

Without changing your read statement, you could do:

echo
cvs commit -m "$REPLY" filename.txt    # double-quote or kill expansion

The proper format for your read is:

read -p "Please enter what you did here: " SOMEVAR

Whatever the user enters, regardless of whether it has spaces, will end up in SOMEVAR. You can then:

echo
cvs commit -m "$SOMEVAR" filename.txt    # double-quote or kill expansion

Upvotes: 2

meuh
meuh

Reputation: 12255

You meant

read -ep "Please enter what you did here: "
echo
cvs commit -m "$REPLY" filename.txt

Upvotes: 1

Nikita Sivukhin
Nikita Sivukhin

Reputation: 2605

You can use command argument -a array of read function

read -a arr -p "Hello "
echo ${arr[*]}

Upvotes: 0

Related Questions