1

So according to this, processes do not inherit shell variables.

So this process should not see variables A and B:

A=X
B=Y
java -cp blah ...

This all make sense until you see that they are passed if defined on the same line:

A=X \
B=Y \
java -cp blah ...

Is this specific to Java (perhaps internally reading command line) or to all *nix processes?

Aliostad
  • 80,612
  • 21
  • 160
  • 208

2 Answers2

4

It's not specific to java but the posix shell grammar rules. When the variable are on the same line it's assignment preceding command. Whereas on different lines the child process can see them only if exported export.

http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_10_02

  1. [Assignment preceding command name]

    [When the first word]

    If the TOKEN does not contain the character '=', rule 1 is applied. Otherwise, 7b shall be applied.

    [Not the first word]

    If the TOKEN contains the equal sign character:

    If it begins with '=', the token WORD shall be returned.
    
    If all the characters preceding '=' form a valid name (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 3.230, Name),
    

    the token ASSIGNMENT_WORD shall be returned. (Quoted characters cannot participate in forming a valid name.)

    Otherwise, it is unspecified whether it is ASSIGNMENT_WORD or WORD that is returned.
    

Assignment to the NAME shall occur as specified in Simple Commands.

Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • Obviously the better answer. As I am closing to the daily limit today, I am inclined to keep mine around; at least for today ;-) – GhostCat Jun 22 '17 at 13:59
  • @nahuel-fouilleul so does it make a difference if defined before or after calling the executable? – Aliostad Jun 22 '17 at 16:19
1

Turning things into a single line turns it into a single process call. In other words: you are basically saying: evaluate this expression as a whole; and the expression starts with those two definitions.

GhostCat
  • 137,827
  • 25
  • 176
  • 248