Backslashes in Command Line Arguments

J

Joseph Pecoraro

In writing a script that takes strings on the command line I have run
into a small problem which I don't see any way around. Here is a very
simplified program that just prints out the first command line argument,
and using p you can see exactly what is in the string.

[example.rb]
<code>p $*[0]</code>


Now when I send some different (but similar) strings I end up with the
same result in the ARGV array:

$ ruby example.rb "\a"
"\\a"

$ ruby example.rb "\\a"
"\\a"

$ ruby example.rb \\\a
"\\a"

1) Is this because I am running in bash and the shell maps the \\ into a
single backslash?
2) Is there any way around this in Ruby or from the command line (in
bash) so that I can differentiate between these?

Thanks,
Joseph Pecoraro
 
S

Sebastian Hungerecker

Joseph said:
1) Is this because I am running in bash and the shell maps the \\ into a
single backslash?
Yes.

2) Is there any way around this in Ruby or from the command line (in
bash) so that I can differentiate between these?

~> echo \\
\
~> echo "\\"
\
~> echo '\\'
\\


HTH,
Sebastian
 
J

Joseph Pecoraro

Sebastian said:
~> echo \\
\
~> echo "\\"
\
~> echo '\\'
\\


HTH,
Sebastian

Sadly, passing those into example.rb to Ruby produces the same
undesirable results. Thanks for your help, I didn't know that feature
about single quoted strings in bash.
 
S

Sebastian Hungerecker

Joseph said:
Sadly, passing those into example.rb to Ruby produces the same
undesirable results.

It does?
~> echo "p ARGV[0]">t.rb

~> ruby t.rb '\'
"\\"

~> ruby t.rb '\\'
"\\\\"

~> ruby t.rb '\\\'
"\\\\\\"
 
R

Rick DeNatale

In writing a script that takes strings on the command line I have run
into a small problem which I don't see any way around. Here is a very
simplified program that just prints out the first command line argument,
and using p you can see exactly what is in the string.

[example.rb]
<code>p $*[0]</code>


Now when I send some different (but similar) strings I end up with the
same result in the ARGV array:

$ ruby example.rb "\a"
"\\a"

$ ruby example.rb "\\a"
"\\a"

$ ruby example.rb \\\a
"\\a"

1) Is this because I am running in bash and the shell maps the \\ into a
single backslash?

No, it's because you are using p instead of puts.

The p method prints the result of sending the inspect message to its
argument, and String#inspect produces a what ruby string literal to
represent the string looks like, and since \ is the escape character
in ruby string literals it needs to be escaped itself in the output
of inspect.
 
J

Joseph Pecoraro

No, it's because you are using p instead of puts.
The p method prints the result of sending the inspect message to its
argument, and String#inspect produces a what ruby string literal to
represent the string looks like, and since \ is the escape character
in ruby string literals it needs to be escaped itself in the output
of inspect.

I purposely used p instead of puts for that exact reason. Which is how
I noticed that if you send "\n" Ruby does not get a newline character,
it gets a backslash n literal.

Joe
 
M

MonkeeSage

I purposely used p instead of puts for that exact reason. Which is how
I noticed that if you send "\n" Ruby does not get a newline character,
it gets a backslash n literal.

Joe

It does get a newline character...try looking at "\n"[0] (or if you're
using 1.9, "\n".ord)...you'll notice it's the ascii ordinal 10...aka
"newline". That was Robert's point. #inspect (and by proxy #p)
represent non-printing characters as their escaped symbols so that you
can actually see them. Try entering this in irb and notice the
difference betwen the input string and that printed by #p...

p "hello\x09there"

Regards,
Jordan
 
J

Joseph Pecoraro

It does get a newline character...try looking at "\n"[0] (or if you're
using 1.9, "\n".ord)...you'll notice it's the ascii ordinal 10...aka
"newline". That was Robert's point. #inspect (and by proxy #p)
represent non-printing characters as their escaped symbols so that you
can actually see them. Try entering this in irb and notice the
difference betwen the input string and that printed by #p...

It seems both of you have missed the point of my question. The input is
coming into Ruby from the Command Line.

Of course "\n"[0] is going to be a newline... IT is a newline. But if
you read the above posts you will see what I was talking about:

$ ruby example.rb "\n"

Does not result in YOUR "\n", it results in "\\n" which was part of MY
problem, which Sebastian showed me how to solve.

I know I have the literal string referring to a Backslash N and not a
Newline because I used #p...

"\n" #=> newline
"\\n" #=> backslash n
 
M

MonkeeSage

It does get a newline character...try looking at "\n"[0] (or if you're
using 1.9, "\n".ord)...you'll notice it's the ascii ordinal 10...aka
"newline". That was Robert's point. #inspect (and by proxy #p)
represent non-printing characters as their escaped symbols so that you
can actually see them. Try entering this in irb and notice the
difference betwen the input string and that printed by #p...

It seems both of you have missed the point of my question. The input is
coming into Ruby from the Command Line.

Of course "\n"[0] is going to be a newline... IT is a newline. But if
you read the above posts you will see what I was talking about:

$ ruby example.rb "\n"

Does not result in YOUR "\n", it results in "\\n" which was part of MY
problem, which Sebastian showed me how to solve.

I know I have the literal string referring to a Backslash N and not a
Newline because I used #p...

"\n" #=> newline
"\\n" #=> backslash n

Ah-ha. I understood your original problem, but I was confused about
your reasoning for using #p...by 'send "\n"' in your previous post I
(mis-)understood 'type "\n" in a script', and thought you were saying
that you used #p because you wanted to see whether ruby would output a
literal newline or keep the two-byte escape sequence. Sorry about
that!

Regards,
Jordan
 
J

Joseph Pecoraro

No problem. Also, after re-reading my previous comment I noticed I
sounded a little bit rude, so I apologize for that. I didn't mean to be
rude. I often misuse CAPS for emphasis and it ends up looking like
yelling instead.

Thanks for everyone's help. I just updated my little script called "rr"
available here <a href="http://bogojoker.com">at my blog</a>. Its
rather amateur but I'll continue to improve it.

Thanks,
Joseph Pecoraro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top