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.

Emma
  • 27,428
  • 11
  • 44
  • 69
Atomiklan
  • 5,164
  • 11
  • 40
  • 62
  • Put a space between the last single quote and the variable name and you’re good to go – x3l51 Mar 06 '19 at 07:20

2 Answers2

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?

Inian
  • 80,270
  • 14
  • 142
  • 161
  • Sorry yes, that was just a typo entering to code in Stack Exchange – Atomiklan Mar 06 '19 at 07:14
  • 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. – Atomiklan Mar 06 '19 at 07:17
  • 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 Mar 06 '19 at 07:24
  • @Atomiklan : Make sure you quote the variable with double quotes – Inian Mar 06 '19 at 07:27
  • 1
    @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. – Gordon Davisson Mar 06 '19 at 07:51
  • 1
    Yup you guys are correct. That was the issue. Thank you! Inian gets the credit, but thank you everyone else for answering! – Atomiklan Mar 06 '19 at 08:09
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.

user1934428
  • 19,864
  • 7
  • 42
  • 87