0

I am struggling to understand how we handle bash read inputs, when the user needs to input data with a space.

Can someone please provide a simple explanation?

read -p 'Artist: ' ARTIST
read -p 'Album: ' ALBUM
read -p 'Genre: ' GENRE

A single word artist (Bjork) will work fine, but as soon as you try to enter a first and last name for example, the entire system crumbles.

I'm guessing the only way I will be able to do this is handle each input individually with a loop and then concentrate the string down into a single variable with the spaces intact.

1
  • Put a space between the last single quote and the variable name and you’re good to go Commented Mar 6, 2019 at 7:20

2 Answers 2

2

You seem to have a typo in the read statement. There should be a space between the terminating quote of -p and the variable placeholder, without the same read squashes the prompt message wrongly as

read -p 'Artist: ARTIST'

where it should have been

read -p 'Artist: ' ARTIST
#               ^^^^

You could see the second one work as expected. Also with proper quotes your script should work as expected

bash script.sh
Artist: John Lenon
Album: The Beatles

puts the result as expected.


The question has been edited since the first post which had a problem with the read statement in quotes. My original answer was intended to answer that. Since then the question was modified which looks more or like Bash: preserve string with spaces input on command line?

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry yes, that was just a typo entering to code in Stack Exchange
Hmm strange... Error must not be with the read, but instead when I pass the variable to function. Checking over everything again. Back in a few mins.
Ok weird, the issue is when I pass the variable to the function. That's where it is getting split up into John and Lenon. So this was a red herring. I'll mark this answered and poke around a bit with the official problem and open a new question if necessary.
@Atomiklan You should almost always put double-quotes around variable references. That is, use "$ARTIST" instead of just $ARTIST. BTW, it's also safer to use lower- or mixed-case variable names, because there are a lot of all-caps variables with special meanings, and if you accidentally use one of them for something else weird things can happen.
Yup you guys are correct. That was the issue. Thank you! Inian gets the credit, but thank you everyone else for answering!
|
0

You did it correctly - see this transcript from my bash:

~ $ read -p 'Artist: ' ARTIST
Artist: Ronald Fischer
~ $ echo $ARTIST
Ronald Fischer
~ $

Verify that you really have bash running, and that, when testing your code, you don't accidentally enter some characters outside the ASCII range; those characters should be tested only after you have verified that spaces in the input do work.

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.