1

any ideas how to get environment variables into dtach? they don't seem to be inherited by default...

FOO=bar && dtach -c /tmp/a-socket env
# env does not include $FOO

[edit]

because of Reasons (TM) I need to have some sort of command separator in there. Either a ; or a &&.

(I appreciate now that this is not a dtach-specific question, it just reflects my poor understanding of shell...)

marked as duplicate by Jesse_b, thrig, Anthony Geoghegan, Jeff Schaller, roaima Jan 1 '18 at 21:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    what's the && for? – thrig Jan 1 '18 at 17:35
  • I needed it because reasons. I now see that's what was causing my problem... – hwjp Jan 1 '18 at 22:14
1

Actually, export is not needed as well. export means that:

Marks each NAME for automatic export to the environment of subsequently executed commands. If VALUE is supplied, assign VALUE before exporting.

You can expose environment variable with the following command schema:

FOO=bar dtach -c /tmp/a-socket env

It will not cause a side effect that next child-process (so executed command) will inherit FOO=bar environment variable.

Commenting your first execution try. It does not make sense to add && operator between configuring environment variable and running the process. It runs nothing (exit code == 0) and then runs your command without environment variable.

1

In your example && is functioning as a command separator (specifically only execute the second command if the first succeeds). This is probably not what you want consider the following examples:

name=value;$command
name=value||$command
name=value&&$command

in all three of these cases name is set to value in the current shell and in the first and third cases the command $command is then executed because assignment will never fail and in the second case $command is not executed because assignment will never fail (possible exceptions in odd shells with reserved variables). In no case is the variable passed to $command.

name=value;export name;$command
export name=value;$command

Omitting similar examples with the other command separators, these examples add an export which passed the variable to $command and every other command executed hereafter in the current session.

name=value $command

Note the absence of a command separator. In this case the variable is passed to $command and only command, it is not used in the current shell.

0

Answering my own question, with apologies: if you really must have an && or ; command separator, then you have no choice but to use export:

export FOO=bar; dtach -c /tmp/a-socket env
# this time env will show FOO=bar

you could add an ; unset FOO at the end if you don't want to pollute your main shell...

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