You are not logged in.

#1 2017-11-26 18:57:19

brianb
Member
From: Montreal, QC
Registered: 2012-02-17
Posts: 81
Website

dwm config: SHCMD vs. defining a command variable

I just switched to dwm and so far am really enjoying it. I just have a quick question: what is difference (if any) between:

static const char *mycmd[] = { "mycmd", "-o", "option", "argument", NULL };

static Key keys[] = {
    ...
    { MODKEY, XK_a, spawn, {.v = mycmd } },
    ...
}

versus:

static Key keys[] = {
    ...
    { MODKEY, XK_a, spawn, SHCMD("mycmd -o option argument") },
    ...
}

?

The latter is made possible due to this line in config.h:

/* helper for spawning shell commands in the pre dwm-5.0 fashion */
#define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }

That comment ("pre dwm-5.0 fashion") makes me think that SHCMD is outdated, or that somehow the first method is better/safer. Is this true? Is there any reason that I should avoid SHCMD, in favor of the first method?

Here's an example case where I think I need SHCMD, because the first option doesn't work: calling a subroutine (like date) within the command, e.g. to take a screenshot with import and name the file with a timestamp:

import -window root "/tmp/$(date '+%Y-%m-%d-%T')-screenshot.png"

This works:

static Key keys[] = {
    ...
    { MODKEY, XK_Print, spawn, SHCMD("import -window root \"/tmp/$(date '+%Y-%m-%d-%T')-screenshot.png\"") },
    ...
}

This doesn't work:

static const char *importcmd[] = { "import", "-window", "root", "\"/tmp/$(date '+%Y-%m-%d-%T')-screenshot.png\"", NULL };

static Key keys[] = {
    ...
    { MODKEY, XK_Print, spawn, {.v = importcmd } },
    ...
}

And I imagine the same holds true for other semi-complex things, like pipes, redirects, etc.

Obviously, I could write a separate script (myimport) that is equivalent to the import command above, then define and call myimportcmd, etc. But that seems needlessly complicated.

Question 1: Are there any downsides to using SHCMD for a use case like the above?

Question 2: Aside from the (subjective) value of separating out commands into variable definitions (and thus making the keybindings array more legible), is there any (substantive) reason not to use SHCMD for every spawn keybinding? It just seems more powerful to me, but maybe I'm missing something important here.

Last edited by brianb (2017-11-26 19:28:03)

Offline

Board footer

Powered by FluxBB