LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2013, 06:27 AM   #1
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
echo cmd collapsing spaces in value


Observe
Code:
s='x   asd                      x'

printf "$s\n" $s
x   asd                      x

printf "%s\n" $s
x
asd
x

echo $s
x asd x

echo "$s"
x   asd                      x
Can anyone explain why 1st echo is collapsing the spaces?
Also, why 2nd printf inserts newlines?

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Last edited by chrism01; 05-31-2013 at 06:37 AM. Reason: Edit: added extra examples
 
Old 05-31-2013, 07:18 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

The first echo collapses the spaces because the shell executes one echo command with many arguments/parameters. The shell parses it as command, and it can be followed by many arguments/paramters and they're separated by one or more arguments. So the echo command gets 3 parameters and knows nothing about the whitespace. For example:

Code:
ls  /home                  /bin     /etc
The number of whitespace characters here doesn't matter. The ls command gets 3 parameters.

The other is the behaviour of printf when you give it more arguments than you specify in the format string. Then it outputs the rest of the arguments with the format string. Same thing as this example:

Code:
printf "The argument is %s.\n" first second third
 
Old 05-31-2013, 08:30 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Original Poster
Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Thinking about it, that makes sense; don't know why I didn't remember that before.
Guess I rarely have used it that way with 'data' that has important spacing ...
Thx
 
Old 05-31-2013, 09:25 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Actually the thing to remember is that unquoted variables undergo word splitting. I feel the ls example is actually a little ambiguous because if you placed quotes before /home and after /etc you would get an error about no such file or directory.

If we look at the examples in order:
Code:
s='x   asd                      x'

# here printf is actually printing the expanded string within the quotes and as there is no format criteria (like %s) the $s outside the quotes is actually being ignored
printf "$s\n" $s
x   asd                      x

# here we have a format identifier, so $s undergoes word splitting due to no quotes hence printf has now been gived 3 strings to print
printf "%s\n" $s
# So this is expanded to: printf "%s\n" x asd x
x
asd
x

# here we also undergo word splitting and so as Guttorm explained, echo is now receiving three arguments similar to printf above
echo $s
# expands to: echo x asd x
x asd x

# lastly, the variable is quoted, hence white space is preserved and echo is passed the string in its original form
echo "$s"
x   asd                      x
 
Old 06-03-2013, 03:59 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Original Poster
Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yeah exactly, I was just having a brain-fade.
Was working on other stuff and using echo (later printf) to just check my other code and was just taking them for granted.
If I'd stopped to think clearly I'd have got that.
It was an 'of course!' moment when I saw Guttorm's answer and your later answer also occurred to me at that time.
 
  


Reply



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
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 08:08 AM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
LXer: Gartner: Windows collapsing under its own weight; Radical change needed LXer Syndicated Linux News 0 04-10-2008 05:30 AM
howto write a script to echo files with spaces in the name dsids Linux - Newbie 1 11-28-2006 07:18 AM
How do I prevent echo eating spaces? essdeeay Linux - General 3 11-01-2005 03:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 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
Open Source Consulting | Domain Registration