Search code examples
awesome-wm

When to use spawn.with_shell and when spawn is only needed?


I'm confused when i should use awful.spawn and when to use awful.spawn.with_shell. To me these look and work the same.
The only difference I see is that in awful.spawn you can set client rules and make a callback.

I would appreciate any examples or rules on when to use each one.


Solution

  • awful.spawn.with_shell really does not do more than spawning the given command with a shell: https://github.com/awesomeWM/awesome/blob/c539e0e4350a42f813952fc28dd8490f42d934b3/lib/awful/spawn.lua#L370-L371

    function spawn.with_shell(cmd)
        if cmd and cmd ~= "" then
            cmd = { util.shell, "-c", cmd }
            return capi.awesome.spawn(cmd, false)
        end
    end
    

    So, why would one want that? Some things are done by shells. For example, output redirections (echo hi > some_file), command sequences (echo 1; echo 2) or pipes (echo hello | grep ell) are all done by a shell. None of these work when starting a process correctly.

    Why would one not want a shell? For example, argument escaping is way more complicated when a shell is involved. When you e.g. want to start print a pipe symbol (no idea why one would need that), then awful.spawn({"echo", "|"}) just works, while with a shell you need to escape the pipe symbol the appropriate number of times. I guess that awful.spawn.with_shell("echo \\\|") would work, but I am not sure and this is the point.

    Also, a shell that does nothing is an extra process and is a tiny bit slower than without a shell, but this difference is really unimportant.