# Default shell for Kernel::system()?

**URL:** <https://rubytalk.org/t/default-shell-for-kernel-system/29499>\
**Category:** ruby-talk\
**Created:** [1 August 2006 08:20 UTC](https://rubytalk.org/t/default-shell-for-kernel-system/29499 "2006-08-01T08:20:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronald\_Fischer2](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ronald\_Fischer2](https://rubytalk.org/u/Ronald_Fischer2)\
**Post date:** [1 August 2006 08:20 UTC](https://rubytalk.org/t/default-shell-for-kernel-system/29499/1 "2006-08-01T08:20:25Z")

</div>

Ist it possible to kind of define a default shell which will be used  
for Kernel::system? I often need to invoke from my Ruby scripts  
external commands, involving redirection and globbing, for example  
(bash syntax)

&nbsp;&nbsp;&nbsp;&nbsp;foo [a-z]\* 2\>&-  
&nbsp;&nbsp;&nbsp;&nbsp;bar \<\<\<baz

etc. Since these syntactic elements are shell specific, I need to  
specify the shell somehow, for example:

&nbsp;&nbsp;&nbsp;&nbsp;system("bash --norc -c 'foo [a-z]\* 2\>&-'")  
&nbsp;&nbsp;&nbsp;&nbsp;system("bash --norc -c 'bar \<\<\<baz'")

which is a bit cumbersome. Of course one obvious solution would be  
to define a new function:

&nbsp;&nbsp;&nbsp;&nbsp;def bash(cmd)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system("bash --norc -c '"+cmd+"'")  
&nbsp;&nbsp;&nbsp;&nbsp;end

And use instead

&nbsp;&nbsp;&nbsp;&nbsp;bash('foo [a-z]\* 2\>&-')  
&nbsp;&nbsp;&nbsp;&nbsp;bash('bar \<\<\<baz')

But this solution has two drawbacks:

First one has to pay special attention that the shell command itself should no contain single quotes. For example, it is all too tempting  
to write a call

&nbsp;&nbsp;&nbsp;&nbsp;bash("my\_command 'one argument'")

which looks correct at first sight, but will have a different  
effect than

&nbsp;&nbsp;&nbsp;&nbsp;bash('my\_command "one argument"')

due to the interference between Ruby- and bash quoting.

Another drawback of this solution is that one has to include the  
definition of the "bash" function with each Ruby script.

So I wonder whether there is may be a simple, builtin solution  
for this problem - say, a way to specify the "default shell" used  
for system().

Ronald

> **···**
>
> --  
> Ronald Fischer \<[ronaldf@eml.cc](mailto:ronaldf@eml.cc)\>  
> Posted via [http://www.newsoffice.de/](http://www.newsoffice.de/)

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [1 August 2006 12:34 UTC](https://rubytalk.org/t/default-shell-for-kernel-system/29499/2 "2006-08-01T12:34:32Z")

</div>

I'm pretty sure the "default" shell for system is /bin/sh on a \*nix.

> **···**
>
> On Aug 1, 2006, at 4:20 AM, Ronald Fischer wrote:
> 
> > Ist it possible to kind of define a default shell which will be used  
> > for Kernel::system? I often need to invoke from my Ruby scripts  
> > external commands, involving redirection and globbing, for example  
> > (bash syntax)
> > 
> > &nbsp;&nbsp;&nbsp;foo [a-z]\* 2\>&-  
> > &nbsp;&nbsp;&nbsp;bar \<\<\<baz
> > 
> > etc. Since these syntactic elements are shell specific, I need to  
> > specify the shell somehow, for example:
> > 
> > &nbsp;&nbsp;&nbsp;system("bash --norc -c 'foo [a-z]\* 2\>&-'")  
> > &nbsp;&nbsp;&nbsp;system("bash --norc -c 'bar \<\<\<baz'")
> > 
> > which is a bit cumbersome. Of course one obvious solution would be  
> > to define a new function:
> > 
> > &nbsp;&nbsp;&nbsp;def bash(cmd)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system("bash --norc -c '"+cmd+"'")  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > And use instead
> > 
> > &nbsp;&nbsp;&nbsp;bash('foo [a-z]\* 2\>&-')  
> > &nbsp;&nbsp;&nbsp;bash('bar \<\<\<baz')
> > 
> > But this solution has two drawbacks:
> > 
> > First one has to pay special attention that the shell command itself should no contain single quotes. For example, it is all too tempting  
> > to write a call
> > 
> > &nbsp;&nbsp;&nbsp;bash("my\_command 'one argument'")
> > 
> > which looks correct at first sight, but will have a different  
> > effect than
> > 
> > &nbsp;&nbsp;&nbsp;bash('my\_command "one argument"')
> > 
> > due to the interference between Ruby- and bash quoting.
> > 
> > Another drawback of this solution is that one has to include the  
> > definition of the "bash" function with each Ruby script.
> > 
> > So I wonder whether there is may be a simple, builtin solution  
> > for this problem - say, a way to specify the "default shell" used  
> > for system().
> > 
> > Ronald
> > 
> > --  
> > Ronald Fischer \<ronaldf@eml.cc\>  
> > Posted via [http://www.newsoffice.de/](http://www.newsoffice.de/)

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [1 August 2006 13:46 UTC](https://rubytalk.org/t/default-shell-for-kernel-system/29499/3 "2006-08-01T13:46:42Z")

</div>

Hi,

At Tue, 1 Aug 2006 17:20:25 +0900,  
Ronald Fischer wrote in [ruby-talk:205370]:

> Ist it possible to kind of define a default shell which will be used  
> for Kernel::system? I often need to invoke from my Ruby scripts

/bin/sh is hardcoded, like as make, cron and so on.

> external commands, involving redirection and globbing, for example  
> (bash syntax)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;foo [a-z]\* 2\>&-

Closing descriptor should work on all variants of Bourne shell,  
including UNIX Seventh Edition's.

[http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=Unix+Seventh+Edition&format=html](http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=Unix+Seventh+Edition&format=html)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Input output.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<&- The standard input is closed. Similarly for the standard output  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using \>.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If one of the above is preceded by a digit then the file descriptor  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;created is that specified by the digit (instead of the default 0 or 1).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For example,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... 2\>&1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;creates file descriptor 2 to be a duplicate of file descriptor 1.

> &nbsp;&nbsp;&nbsp;&nbsp;bar \<\<\<baz

In Ruby, you can:

&nbsp;&nbsp;IO.popen("bar", "w") {|f| f.puts "baz"}

> &nbsp;&nbsp;&nbsp;&nbsp;def bash(cmd)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system("bash", "--norc", "-c", cmd)

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> --  
> Nobu Nakada
