LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2009, 01:52 PM   #1
gqchynaboy
LQ Newbie
 
Registered: Jul 2003
Posts: 20

Rep: Reputation: 0
"Read" is dropping everything after the white space


read -p "Enter the Project Folder Name >> " projName
echo $projName
cd $HOME/Desktop/$projName/images


So if I input a string "foo bar world"
It would echo "foo bar world"

but when it hits the 'cd' command
It would do this "$HOME/Desktop/foo/images

My question is why is it dropping the rest of the string after the white space? How do I prevent this?
 
Old 08-13-2009, 02:00 PM   #2
KBriggs
Member
 
Registered: Jun 2009
Posts: 35

Rep: Reputation: 15
I think you need to backslash excape character filenames with spaces.

ie.

foo\ bar\ world
 
Old 08-13-2009, 02:02 PM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quotes.

Code:
echo $HOME/Desktop/$projName/images
Means:

Code:
cd $HOME/Desktop/foo var world/images
Where "$HOME/Desktop/foo" is one argument, "var" is another argument, and "world/images" is another argument. The blank space has an special meaning for the shell, it's a separator. You can bypass that by quoting the string, so bash will understand it as a single argument.

Code:
echo "$HOME/Desktop/$projName/images"
You could also quote the relevant part, if you are 100% sure that the rest of the string will never contain spaces:

Code:
echo $HOME/Desktop/"$projName"/images
KBriggs, yes, you could do that, but in this case the string is not known beforehand. Just quoting is more convenient, unless you are willing to process the string after reading it to substitute spaces and other special characters.

Last edited by i92guboj; 08-13-2009 at 02:04 PM.
 
Old 08-13-2009, 02:06 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Read is not the problem.

You need to put double quotes around $HOME/Desktop/$projName/images.

cd $HOME/Desktop/$projName/images is first picked up by bash and both variables ($HOME and $projName) are expanded, after that the command looks like:

cd /home/user/Desktop/foo bar world/Images.

cd has no way of knowing that bar and world/Images are part of /home/user/Desktop/foo and will ignore them. Putting double quotes around it tells cd that it all belongs together: cd "$HOME/Desktop/$projName/images"

Hope this helps.
 
Old 08-13-2009, 02:07 PM   #5
gqchynaboy
LQ Newbie
 
Registered: Jul 2003
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by i92guboj View Post
Quotes.

Code:
echo $HOME/Desktop/$projName/images
Means:

Code:
cd $HOME/Desktop/foo var world/images
Where "$HOME/Desktop/foo" is one argument, "var" is another argument, and "world/images" is another argument. The blank space has an special meaning for the shell, it's a separator. You can bypass that by quoting the string, so bash will understand it as a single argument.

Code:
echo "$HOME/Desktop/$projName/images"
You could also quote the relevant part, if you are 100% sure that the rest of the string will never contain spaces:

Code:
echo $HOME/Desktop/"$projName"/images
KBriggs, yes, you could do that, but in this case the string is not known beforehand. Just quoting is more convenient, unless you are willing to process the string after reading it to substitute spaces and other special characters.
Thats what I was looking for,

Thanks, that was a big help!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting "Short read in buffer_copy" error with "dpkg" stalefries Linux - Software 6 03-24-2010 04:19 PM
How to read "identify" button press event, or state of "identify" blue led with IPMI? iav Linux - Server 0 01-27-2009 02:13 PM
"Hiding a process" - yes, a "white hat" angle sundialsvcs Programming 2 12-20-2007 02:32 PM
Screen saver blank (or white "X") on kubuntu (edgy) lordgreggreg Linux - Newbie 5 04-07-2007 12:11 PM
Edit "read-only" /etc/fstab from "System Recovery#" (without boot cd?) charlweed Linux - General 1 12-31-2006 06:57 PM


All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration