The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Escaping the * character in ksh. arvindcgi Shell Programming and Scripting 6 05-19-2008 06:50 AM
Escaping specific character in awk brainyoung Shell Programming and Scripting 4 01-28-2008 09:42 AM
Escaping '*' in Bash rkshukla14 Shell Programming and Scripting 7 04-04-2007 08:45 PM
bash script help: escaping the '*' mark_nsx Shell Programming and Scripting 18 10-07-2005 05:20 AM
escaping * in korn shell prekida Shell Programming and Scripting 3 07-13-2005 02:19 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-30-2007
Registered User
 

Join Date: Dec 2007
Posts: 102
Stumble this Post!
problem escaping newline in ksh

Hi,

I did the below.

$ print "\\n"


$


I am curious, why does \\n give two new lines? I would have thought that the first \ would escape the second \, and so we'd get \n printed. But we didn't.

Any ideas?

Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-30-2007
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 946
Stumble this Post!
To get "\n" printed you need to add another \

Code:
print "\\\n"
Reply With Quote
  #3 (permalink)  
Old 12-30-2007
Registered User
 

Join Date: Dec 2007
Posts: 102
Stumble this Post!
Quote:
Originally Posted by fpmurphy View Post
To get "\n" printed you need to add another \

Code:
print "\\\n"
Yes, you are right.

But why does "\\n" not print \n?
Reply With Quote
  #4 (permalink)  
Old 01-01-2008
Registered User
 

Join Date: Dec 2007
Posts: 102
Stumble this Post!
Any update, anyone?
Reply With Quote
  #5 (permalink)  
Old 01-01-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,430
Stumble this Post!
OK,
print "\\n"

The shell peeks inside double quoted strings and processes stuff like variables and command substitution. During this inside peek, a backslash may be treated specially if it is followed one of these 4 characters: $`\" In other words, inside double quotes a backslash is used only to turn off the few characters that otherwise would be treated specially. So in the line,
somecommand "\n"
the shell treats the backslash as just a literal character since it is not followed by a character to interact with. So the command called "somecommand" is run with a single argument of just \n. Now in the case of a line like:
somecommand "\\n"
the first backslash interacts with the second backslash and prevents it from being special. It was destined to not be special anyway since it was not followed by one of the 4 characters it can interact with. But it is fine to explicitly make it non-special with an extra backslash. I usually do this to make the code more obvious.

So in either case, a command is being run with an argument of \n, but now we need to ask what the command will do with that argument. The print command, by default, processes some backslash combos as well. And yes, \n is one. You could use "print -r" or "print -R" to inhibit this.
Reply With Quote
  #6 (permalink)  
Old 01-05-2008
Registered User
 

Join Date: Dec 2007
Posts: 102
Stumble this Post!
Quote:
Originally Posted by Perderabo View Post
OK,
print "\\n"

The shell peeks inside double quoted strings and processes stuff like variables and command substitution. During this inside peek, a backslash may be treated specially if it is followed one of these 4 characters: $`\" In other words, inside double quotes a backslash is used only to turn off the few characters that otherwise would be treated specially. So in the line,
somecommand "\n"
the shell treats the backslash as just a literal character since it is not followed by a character to interact with. So the command called "somecommand" is run with a single argument of just \n. Now in the case of a line like:
somecommand "\\n"
the first backslash interacts with the second backslash and prevents it from being special. It was destined to not be special anyway since it was not followed by one of the 4 characters it can interact with. But it is fine to explicitly make it non-special with an extra backslash. I usually do this to make the code more obvious.

So in either case, a command is being run with an argument of \n, but now we need to ask what the command will do with that argument. The print command, by default, processes some backslash combos as well. And yes, \n is one. You could use "print -r" or "print -R" to inhibit this.


That is a great reply. echo in tcsh gives very different results, so that was really confusing me!

So to clarify, for
Code:
print "\n"
...the shell simply interprets this as literally \n, and sends this to print. For print, a literal \n means newline, so it prints a newline. Yes?

For
Code:
print "\\n",
the shell treats the first \ as special, because it is followed by a \. The shell takes this to mean that treat the second \ as non-special (which it already is). So the shell once again sends \n to print, and once again we get a newline. Yes?

For
Code:
print "\\\n"
the shell sees the first \ followed by a \, so the first \ is special. The second \ is not special, as it was escaped. The third \ is also not special, as it is followed by a n. So, the shell sends \\n as argument to print. Now print sees \\n as meaning do not treat \n as a newline, and so it prints \n onto the screen. Yes?

(Also, you said the shell only considers \ to be special if it is followed by \ , $ , " or `. So to shell \n does not mean a newline?)

Thanks.
Reply With Quote
  #7 (permalink)  
Old 01-05-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,430
Stumble this Post!
Yes, your understanding is correct.

The only thing is that "print" is a built-in command to the shell. So what print does is really done by a part of the shell.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0