system command and echo strangeness

22 views
Skip to first unread message

Colin Law

unread,
Mar 8, 2016, 10:49:23 AM3/8/16
to Ruby on Rails: Talk
Can anyone explain this?

If in a terminal I run echo -e 'hello' I see, as one would expect,
$ echo -e 'hello'
hello

However if I execute the command using irb I see
$ irb
2.1.3 :001 > system "echo -e 'hello'"
-e hello
=> true

It echoes the option which suggests to me that echo does not recognise -e
If however I do
2.1.3 :003 > system "/bin/echo -e 'hello'"
hello
=> true

Which implies that when I do not specify the path it is using a
different version of echo.

I also see
2.1.3 :005 > system "which echo"
/bin/echo
=> true

But I am not sure that is relevant. Google has not helped me in this matter.

This is using gnome-terminal in Ubuntu 16.04 (and 14.04)

Any suggestions anyone?

Colin

Walther Diechmann

unread,
Mar 8, 2016, 10:57:45 AM3/8/16
to Ruby
what does

ruby -e "system 'which echo’"

tell you?


/Walther
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuN%3DD%2Bgug1OkOATKeZgYbqXdYuq%2BCJArrFQPpDVriXt%3Dw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Denne besked er blevet skannet af ALCO Spamstop og menes at være fri for vira og spam.
>

Hassan Schroeder

unread,
Mar 8, 2016, 11:03:23 AM3/8/16
to rubyonrails-talk
On Tue, Mar 8, 2016 at 7:48 AM, Colin Law <cla...@gmail.com> wrote:

> Which implies that when I do not specify the path it is using a
> different version of echo.

It is - google "bash builtin commands" (assuming you're using bash
as your default shell).

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

nanaya

unread,
Mar 8, 2016, 11:39:14 AM3/8/16
to rubyonra...@googlegroups.com
Hi,

On Wed, Mar 9, 2016, at 01:02, Hassan Schroeder wrote:
> On Tue, Mar 8, 2016 at 7:48 AM, Colin Law <cla...@gmail.com> wrote:
>
> > Which implies that when I do not specify the path it is using a
> > different version of echo.
>
> It is - google "bash builtin commands" (assuming you're using bash
> as your default shell).
>

it's actually more than that:

irb(main):003:0> system "echo -e hello"
hello
=> true
irb(main):004:0> system "echo -e 'hello'"
-e hello
=> true
irb(main):005:0>
$ sudo mv /bin/echo /bin/echo.1
...
$ irb
irb(main):001:0> system "echo -e hello"
=> nil
irb(main):002:0> system "echo -e 'hello'"
-e hello
=> true
irb(main):003:0>

Looks like adding quote to the command string causes it to be executed
differently.

My guess says that if there's quote in the string at all it is executed
by spawning a subshell while if there's none it's directly executed
through execve() or whatever libc function for it.

observe:

irb(main):009:0> system 'bogus command'
=> nil
irb(main):010:0> system 'bogus "command"'
sh: 1: bogus: not found
=> false


If my quick lookup is correct, it's handled by rb_exec_fillarg function
in process.c - it decides whether or not to spawn shell or just exec
immediately (use_shell) by checking the string against various shell
special keywords and characters.

Colin Law

unread,
Mar 8, 2016, 12:08:41 PM3/8/16
to Ruby on Rails: Talk
Thanks for that explanation, that all seems to make sense. Also
running sh in a terminal shows that the sh builtin echo does not
support -e. It can be a liitle confusing that there are three
versions of echo on the system, builtin commands in sh and bash, and
also /bin/echo.

Thanks again

Colin

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1457455127.2747427.543157010.165C4717%40webmail.messagingengine.com.
Reply all
Reply to author
Forward
0 new messages