problem escaping newline in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem escaping newline in ksh
# 1  
Old 12-30-2007
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.
# 2  
Old 12-30-2007
To get "\n" printed you need to add another \

Code:
print "\\\n"

# 3  
Old 12-30-2007
Quote:
Originally Posted by fpmurphy
To get "\n" printed you need to add another \

Code:
print "\\\n"

Yes, you are right.

But why does "\\n" not print \n?
# 4  
Old 01-01-2008
Any update, anyone?
# 5  
Old 01-01-2008
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.
# 6  
Old 01-05-2008
Quote:
Originally Posted by Perderabo
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.
# 7  
Old 01-05-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escaping a variable in ksh

I am trying to echo a variable exactly to a script- echo "${var1} ${var2} >> output.output Gives me a blank file. I would like output.output to basically say: ${var1} ${var2} I think I need to use a special escape character for variables. Am I right in assuming that, and is it the... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. Shell Programming and Scripting

Escaping problem in a shell script

Hi, i made a gnuplot script which accepts a filename as parameter (using gnuplot -e) now i want to run this script from a shell script, the correct command (with a concrete parameter) looks like this: gnuplot -e 'name="filename.dat;col=2"' gplscript.gpl my shell script looks like this: ... (4 Replies)
Discussion started by: franko007
4 Replies

3. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

4. Shell Programming and Scripting

sed newline to tab ,problem

Input: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly sed 's/\n/\t/g' infile It's not working. Output should be: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly (5 Replies)
Discussion started by: cola
5 Replies

5. UNIX for Dummies Questions & Answers

ksh93 newline character problem

Hi , I am working on Linux, with ksh93 installed. The following statement echo \\n just prints \n on this shell. However on pdksh echo \\n gives a blank new line as a output. What is the correct way of printing a new line character on the ksh93? (4 Replies)
Discussion started by: neeto
4 Replies

6. Shell Programming and Scripting

Newline charachter in Ksh-Linux

Hello, I'm trying to create a muliti value shell variable with newlines inside it, So that I can read the values of that variable individually line by line, but KSH seems to be stripping my variable of newlines in LINUX, but UNIX its working fine. Here's Example : String =... (3 Replies)
Discussion started by: Sanju1236
3 Replies

7. Shell Programming and Scripting

Problem using egrep: spaces and newline

Hello: I am working in bash and am a newbie. I want to eliminate spaces from strings. Since this is a basic operation, I searched online and implemented the suggestions; however, I am facing a problem here. I have an input file which looks like this: abc defghi jklmno pqrs tuvw xyzabcd... (8 Replies)
Discussion started by: andyu11
8 Replies

8. Shell Programming and Scripting

sed ksh remove newline between 2 containers

If we assume that each line between the {} container is an XML document then What I want to remove the newline character from all lines within each container to have one XMDL document per line I wrote a bit of sed after trawling the web: e.g. #!/bin/sed -nf H /}/ { x s/\n//g p... (3 Replies)
Discussion started by: JamesJSC
3 Replies

9. Shell Programming and Scripting

Escaping the * character in ksh.

Hi All, In ksh script i'm trying to assign "sqlstmt1" varaible value, update VAREntryTb set VAR10num = VAR1num * Mltplr where BusD = '$val1' and RunI = 1"` Hence i wrote below statement, the issue with this is shell is expanding "*" character adn thus subistuting it with the content of my... (6 Replies)
Discussion started by: arvindcgi
6 Replies

10. Shell Programming and Scripting

Newline character not working for ksh

Newline character "\n" not working for ksh in linux AS 3.0 Command : $echo "Hi\nHi" $Hi\nHi $ Expected output : $echo "Hi\nHi" Hi Hi $ Can some help me on this Thanks in advance Sanish. (11 Replies)
Discussion started by: sanikv
11 Replies
Login or Register to Ask a Question