You describe 3 cases: your original code, your posted test snippet (with ls), and the test snippet with ls replaced by exec. From your description, it's not clear to me which of the three are failing for you. The last two both work for me (at least if I set $domain to something).