Bash – Passing quotes and full strings to bash -c or zsh -c

bashperlzsh

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.

Best Answer

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'");
Related Question