Backslashes in Command Line Arguments

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]
p $*[0]

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 P.

Joseph P. wrote:

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

Yes.

  1. 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

Sebastian H. wrote:

Joseph P. wrote:

  1. 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

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.

Joseph P. wrote:

Sebastian H. wrote:

~> echo ‘\’
\

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 ‘\’
“\\\”

You are absolutely right, I don’t know what I was thinking.
Thanks!

On 12/16/07, Joseph P. [email protected] wrote:

same result in the ARGV array:

  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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Dec 17, 11:05 am, Joseph P. [email protected] wrote:

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

Posted viahttp://www.ruby-forum.com/.

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

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.


Rick DeNatale

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

On Dec 17, 11:36 am, MonkeeSage [email protected] wrote:

…That was Robert’s point.

Doh! Sorry Rick…brain said one thing, fingers rebelled.

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

On Dec 17, 11:51 am, Joseph P. [email protected] wrote:

Of course “\n”[0] is going to be a newline… IT is a newline. But if
“\n” #=> newline
“\n” #=> backslash n

Posted viahttp://www.ruby-forum.com/.

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

Corrected Link (html was not allowed):

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 at my blog. Its
rather amateur but I’ll continue to improve it.

Thanks,
Joseph P.