backtick subshell

Discussion in 'Ruby' started by Noah Easterly, Nov 14, 2006.

  1. What subshell does ` (backquote) use to run commands?
     
    Noah Easterly, Nov 14, 2006
    #1
    1. Advertising

  2. Noah Easterly

    matt neuburg Guest

    matt neuburg, Nov 14, 2006
    #2
    1. Advertising

  3. I don't think it's using the $SHELL variable to choose which shell to
    use (and I don't trust all the shells to set $SHELL)

    As an example:

    so here's a command that I get different responses on, depending on
    which shell it's run in

    sh-2.05b$ ls **/*.rb | wc -l
    1
    sh-2.05b$ zsh -c "ls **/*.rb | wc -l"
    12

    And in ruby backticks, I get the sh response

    sh-2.05b$ ruby -e "puts %x{ls **/*.rb | wc -l}"
    1

    even when the $SHELL is set otherwise

    sh-2.05b$ export SHELL=/bin/zsh
    sh-2.05b$ echo $SHELL
    /bin/zsh
    sh-2.05b$ ruby -e "puts %x{echo $SHELL}"
    /bin/zsh
    sh-2.05b$ ruby -e "puts %x{ls **/*.rb | wc -l}"
    1

    or if the ruby script is run by a different shell

    zsh: echo $SHELL
    /bin/zsh
    zsh: ruby -e "puts %x{echo $SHELL}"
    /bin/zsh
    zsh: ruby -e "puts %x{ls **/*.rb | wc -l}"
    1


    So my question remains, how is ruby choosing which shell to use for
    backtick commands? (since in this case it really doesn't seem like it's
    checking $SHELL at all).
    I know I can force a choice of shell from within the backtick (ruby -e
    'puts %x{/bin/sh -c "ls **/*.rb | wc -l"}'), but I want to know where
    ruby's getting its choice of shell from.

    On Nov 14, 2:56 pm, Paul Lutus <> wrote:
    > Noah Easterly wrote:
    > > What subshell does ` (backquote) use to run commands?$ irb

    > irb(main):001:0> `echo $SHELL`
    > => "/bin/bash\n"
    >
    > YMMV, depending on platform and version.
    >
    > --
    > Paul Lutushttp://www.arachnoid.com
     
    Noah Easterly, Nov 14, 2006
    #3
  4. Noah Easterly

    Nate Wiger Guest

    Noah Easterly wrote:
    > I don't think it's using the $SHELL variable to choose which shell to
    > use (and I don't trust all the shells to set $SHELL)
    >
    > As an example:
    >
    > so here's a command that I get different responses on, depending on
    > which shell it's run in
    >
    > sh-2.05b$ ls **/*.rb | wc -l
    > 1
    > sh-2.05b$ zsh -c "ls **/*.rb | wc -l"
    > 12
    >
    > And in ruby backticks, I get the sh response
    >
    > sh-2.05b$ ruby -e "puts %x{ls **/*.rb | wc -l}"
    > 1


    How about

    ruby -e "puts %x{zsh -c 'ls **/*.rb' | wc -l}"

    I would not trust ruby to figure out the "correct" shell. I would not
    even be surprised if it was hardwired to /bin/sh for consistent behavior.

    -Nate
     
    Nate Wiger, Nov 14, 2006
    #4
  5. Ok, from skimming process.c in the ruby source, it looks like it's hard
    coded to use 'sh', either through
    calls to system (implicitly, since it forwards to sh by ISO standards),
    execl (explicitly), or spawnl (explicitly),
    except in the case of WIN_32, though, when it calls do_spawn, which i
    think checks the RUBYSHELL environment variable.

    Which is confusing. Can anyone lend me some clarity?

    On Nov 14, 3:49 pm, "Noah Easterly" <> wrote:
    > I don't think it's using the $SHELL variable to choose which shell to
    > use (and I don't trust all the shells to set $SHELL)
    >
    > As an example:
    >
    > so here's a command that I get different responses on, depending on
    > which shell it's run in
    >
    > sh-2.05b$ ls **/*.rb | wc -l
    > 1
    > sh-2.05b$ zsh -c "ls **/*.rb | wc -l"
    > 12
    >
    > And in ruby backticks, I get the sh response
    >
    > sh-2.05b$ ruby -e "puts %x{ls **/*.rb | wc -l}"
    > 1
    >
    > even when the $SHELL is set otherwise
    >
    > sh-2.05b$ export SHELL=/bin/zsh
    > sh-2.05b$ echo $SHELL
    > /bin/zsh
    > sh-2.05b$ ruby -e "puts %x{echo $SHELL}"
    > /bin/zsh
    > sh-2.05b$ ruby -e "puts %x{ls **/*.rb | wc -l}"
    > 1
    >
    > or if the ruby script is run by a different shell
    >
    > zsh: echo $SHELL
    > /bin/zsh
    > zsh: ruby -e "puts %x{echo $SHELL}"
    > /bin/zsh
    > zsh: ruby -e "puts %x{ls **/*.rb | wc -l}"
    > 1
    >
    > So my question remains, how is ruby choosing which shell to use for
    > backtick commands? (since in this case it really doesn't seem like it's
    > checking $SHELL at all).
    > I know I can force a choice of shell from within the backtick (ruby -e
    > 'puts %x{/bin/sh -c "ls **/*.rb | wc -l"}'), but I want to know where
    > ruby's getting its choice of shell from.
    >
    > On Nov 14, 2:56 pm, Paul Lutus <> wrote:
    >
    > > Noah Easterly wrote:
    > > > What subshell does ` (backquote) use to run commands?$ irb

    > > irb(main):001:0> `echo $SHELL`
    > > => "/bin/bash\n"

    >
    > > YMMV, depending on platform and version.

    >
    > > --
    > > Paul Lutushttp://www.arachnoid.com
     
    Noah Easterly, Nov 14, 2006
    #5
  6. Noah Easterly

    powlow Guest

    On Solaris it does seem to just use the sh shell. I tested it out with
    some export commands (which work under ksh, the shell reported by `echo
    $SHELL`) and errors ocurred. If, however, I did it the sh way with
    setting a variable and seperately exporting and then echoing, it worked
    which seems to suggest sh.

    irb(main):001:0> `echo $SHELL`
    => "/bin/ksh\n"
    irb(main):002:0> `export TEST=test; echo $TEST`
    sh: TEST=test: is not an identifier
    => ""
    irb(main):003:0> `TEST=test; export TEST; echo $TEST`
    => "test\n"

    Hope that helps

    On Nov 14, 8:14 pm, "Noah Easterly" <> wrote:
    > What subshell does ` (backquote) use to run commands?
     
    powlow, Nov 23, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leszek Dubiel
    Replies:
    3
    Views:
    9,954
  2. Carl Banks

    Backtick Virgins

    Carl Banks, Aug 21, 2003, in forum: Python
    Replies:
    1
    Views:
    344
    Peter Hansen
    Aug 21, 2003
  3. Tobias Reif

    backtick calls on Windows (9*)

    Tobias Reif, Sep 4, 2003, in forum: Ruby
    Replies:
    0
    Views:
    206
    Tobias Reif
    Sep 4, 2003
  4. Luke Kanies

    UID/EUID subshell solutions

    Luke Kanies, Dec 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    148
    Luke Kanies
    Dec 19, 2006
  5. Ittay Dror
    Replies:
    1
    Views:
    152
    Ittay Dror
    Oct 21, 2008
Loading...

Share This Page