Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-15-2010, 12:55 PM   #1
LQ Newbie
Registered: Jun 2010
Posts: 6

Rep: Reputation: 0
printf white space issue word splitting

I am having trouble keeping the name together and the phone number together, I think due to the white space. I have tryed "" and '' it doesn' seem to matter. So it may be my syntax? and does it matter how long the first and last names are.

me$ echo 'fstname lstname' '123 123-1234' | ./myscript

read a b
printf "%-15s %20s\n" $a $b >> my_phone_numbers


fstname lstname
123 123-1234
insted of
fstname lstname 123 123-1234

i know its not an elegant script but im still learning how some commands work

thanks to the people who help on this site it has helped many times in the past
Old 06-15-2010, 01:36 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
The need for quoting on the command line is a separate issue from the word splitting done inside the script. They're necessary to keep the input strings together when sending the values to the script, but they're not part of the input itself.

Inside the script you have to reapply the quotes wherever you need the spaces to be seen as part of the string, around variable references in particular.
read a b
printf "%-15s %20s\n" "$a" "$b" >> my_phone_numbers
Edit: I just noticed that there's a second issue here as well. The quotes in the input line only protect the input long enough to pass the values to echo; then it's echo that pipes them into the script. This means that the spaces are NOT protected when they hit read. You can check it yourself:
read a b
echo "a is $a"
echo "b is $b"
printf "%-15s %20s\n" "$a" "$b" >> my_phone_numbers
...and the output:
$ echo 'fstname lstname' '123 123-1234' | ./myscript
a is fstname
b is lstname 123 123-1234
firstname       lastname 123 123-1234
The only way I know to overcome that, as long as you use echo and a pipe, is to escape the spaces in order to protect them from echo.
$ echo -e "fstname\ lstname" "123\ 123-1234" | ./myscript
a is fstname lstname
b is 123 123-1234
firstname lastname         123 123-1234

Last edited by David the H.; 06-15-2010 at 02:12 PM.
1 members found this post helpful.
Old 06-15-2010, 02:46 PM   #3
LQ Newbie
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

thanks again for all the help, That did the trick


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
[SOLVED] Bash: why is word splitting not effective in an assignment catkin Programming 5 12-12-2009 01:02 PM
LXer: White Space LXer Syndicated Linux News 0 09-22-2007 09:40 PM
m4 and white space, does it exist? The_Nerd Programming 3 04-18-2007 06:19 PM
removing white space accent11 Linux - Software 4 10-06-2004 02:30 AM
Two Tuxes with block of white space??? robbow52 Debian 1 07-04-2004 02:05 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:17 AM.

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