# Executing systems commands

**URL:** <https://www.ruby-forum.com/t/executing-systems-commands/135714>\
**Category:** Ruby\
**Created:** [April 24, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/executing-systems-commands/135714 "2008-04-24T12:20:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 24, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/executing-systems-commands/135714/1 "2008-04-24T12:20:28Z")

</div>

=begin  
Hey chaps.

I’m using an array to find the locations of various commands on a  
system. Three versions of the same thing. Two work and one doesn’t. Any  
one  
know why?  
=end

# here’s an array of commands that we want to find

k = %w[ruby xterm enlightenment emacs]

#why does this work?  
k.each do |x|  
p system(“which #{x}”)  
end

# and this work

k.each do |x|  
p system(“whereis #{x}”)  
end

# but this doesn’t

k.each do |x|  
p system(“type -p #{x}”)  
end

**END**  
Regards,

- jjm

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 24, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/executing-systems-commands/135714/2 "2008-04-24T13:40:47Z")

</div>

John M. ha scritto:

> # but this doesn’t
> 
> k.each do |x|  
> p system(“type -p #{x}”)  
> end
> 
> **END**  
> Regards,

Just a hint (maybe wrong): could this depend on the fact that “type”  
is a  
shell builtin, while “which” and “whereis” are external commands?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 24, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/executing-systems-commands/135714/3 "2008-04-24T14:35:53Z")

</div>

- John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (12:19) schrieb:

> #why does this work?  
> k.each do |x|  
> p system(“which #{x}”)  
> end
> 
> # and this work
> 
> k.each do |x|  
> p system(“whereis #{x}”)  
> end

which and whereis are real programs

> # but this doesn’t
> 
> k.each do |x|  
> p system(“type -p #{x}”)  
> end

whereas type is a shell builtin. On my system the standard shell is  
dash,  
which doesn’t recognize the -p switch.

NB: system just returns true or false, the output of the command is not  
passed to ruby. Use %x for that.

mfg, simon … l
