Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which shell ruby uses as its subshell?

Tags:

ruby

If I do

`echo $SHELL`

, I get /bin/bash.

But if I try to run this loop:

`for x in {1..20}; do echo $x; done`

I get {1..20} instead of expected 20 numbers.

Maybe ruby uses some other shell to run those commands? How can I work around this?

EDIT: Software versions:

$ irb --version
irb 0.9.5(05/04/13)
$ ruby --version
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
like image 879
Rogach Avatar asked Sep 27 '12 20:09

Rogach


People also ask

What is a subshell in shell?

Definition: A subshell is a child process launched by a shell (or shell script). A subshell is a separate instance of the command processor -- the shell that gives you the prompt at the console or in an xterm window.

How do you create a subshell?

You can also create subshell by launching new shells from your existing shells. Just run bash and you'll be in a subshell. You can use the exit command to close/exit the shell and move back to the original shell.

What is a child shell in Linux?

When you run a program in your shell, a process is created. This new process is called a child process of the shell. The originating process (the shell from which you ran the command) is called the parent process of the child. When you run a new shell, you are creating a child process under the originating shell.

What is the command used to make variables available to subshells?

To make a variable available in a subshell (or any other subprogram of the shell), we have to “export” the variable with the export command.


1 Answers

Ruby uses sh as the subshell for backticks and #system. $SHELL is your default shell, $0 should tell you your current shell. You can get your desired shell by specifically invoking it

$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2011.03
$ irb --version
irb 0.9.5(05/04/13)
$ irb
irb(main):001:0> `echo $0 -- $SHELL`
=> "sh -- /bin/bash\n"
irb(main):002:0> `bash -c 'echo $0 -- $SHELL'`
=> "bash -- /bin/bash\n"
irb(main):003:0> ENV['SHELL']
=> "/bin/bash"
irb(main):004:0> system 'bash', '-c', 'echo $0 -- $SHELL'
bash -- /bin/bash
=> true
irb(main):005:0> system 'echo $0 -- $SHELL'
sh -- /bin/bash
=> true
like image 190
dbenhur Avatar answered Nov 14 '22 23:11

dbenhur