# Default shell for Kernel::system()?

**URL:** <https://www.ruby-forum.com/t/default-shell-for-kernel-system/100820>\
**Category:** Ruby\
**Created:** [August 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/default-shell-for-kernel-system/100820 "2006-08-01T10:52:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/default-shell-for-kernel-system/100820/1 "2006-08-01T10:52:50Z")

</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)

```
foo [a-z]* 2>&-
bar <<<baz

```

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

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

```

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

```
def bash(cmd)
  system("bash --norc -c '"+cmd+"'")
end

```

And use instead

```
bash('foo [a-z]* 2>&-')
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

```
bash("my_command 'one argument'")

```

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

```
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

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 1, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/default-shell-for-kernel-system/100820/2 "2006-08-01T14:36:03Z")

</div>

On Aug 1, 2006, at 4:20 AM, Ronald F. wrote:

> And use instead
> 
> definition of the “bash” function with each Ruby script.

I’m pretty sure the “default” shell for system is /bin/sh on a \*nix.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 1, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/default-shell-for-kernel-system/100820/3 "2006-08-01T15:49:21Z")

</div>

Hi,

At Tue, 1 Aug 2006 17:20:25 +0900,  
Ronald F. 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)
> 
> ```
> 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)

```
   Input output.

   <&- The standard input is closed. Similarly for the standard 

```

output  
using \>.

```
   If one of the above is preceded by a digit then the file 

```

descriptor  
created is that specified by the digit (instead of the default 0  
or 1).  
For example,

```
    ... 2>&1

   creates file descriptor 2 to be a duplicate of file descriptor 1.

```

> ```
> bar <<<baz
> 
> ```

In Ruby, you can:

IO.popen(“bar”, “w”) {|f| f.puts “baz”}

> ```
> def bash(cmd)
> 
> ```

```
    system("bash", "--norc", "-c", cmd)

```
