0

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 :)

3
  • 1
    cvs commit -m "$READ" filename.txt should work. Commented Aug 28, 2015 at 7:52
  • 1
    read -p "Please enter what you did here: " -r READ Then as anubhava said, put it in double quotes. Commented Aug 28, 2015 at 7:55
  • @Ryan Cauldwell: Where do you fill $READ? Commented Aug 28, 2015 at 7:56

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

You meant

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

1 Comment

From help read: If no NAMEs are supplied, the line read is stored in the REPLY variable.
0

You can use command argument -a array of read function

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.