Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I am in Perl and I call system(some_command), this invokes some_command on /bin/sh. I would like it instead to run some_command in another shell such as bash or zsh.

One way of running commands on zsh or bash from sh is to call zsh -c '<command>'. The idea would be to pass this to system(), but this requires using single quotes around <command>.

But what if <command> already has quotes in it? How do I pass it to zsh -c? For example:

find . -name 'something*.txt'

The example above is just for illustrative purpose. Another example would be when I need to pass something with quotes to a binary that I want to invoke from bash or zsh.

share|improve this question
add comment

2 Answers

Use system LIST, which doesn't invoke a shell. i.e.

system('/bin/bash', '-c', $your_command);

instead of

system("/bin/bash -c '$your_command'");
share|improve this answer
    
+1 better answer than mine –  glenn jackman May 24 '13 at 19:57
    
Thanks! Is there a way to pass the current environment variables to that shell?. Would system('source', '/bin/zsh', '-c', $full_cmd') work? –  user815423426 May 24 '13 at 20:08
    
You don't need to do anything to pass the current environment to a child process. You'd have to do something if you didn't want that shell to inherit your environment. –  cjm May 24 '13 at 20:10
    
Hmm I get: Can't exec '/bin/bash': Not a directory when I try system('/bin/bash', '-c', 'echo Hello world'); even though it's certainly there. –  user815423426 May 24 '13 at 22:36
    
@user815423426, it works fine for me. Does system("/bin/bash -c 'echo Hello world'"); work? –  cjm May 24 '13 at 23:11
add comment

Since you're in Perl, you're not limited to single and double quotes:

system q{bash -c 'echo "Hello World"'};
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.