0

I tryied using JAVA_OPTS=-Xms1024m -Xmx1024m ... in a script file. It shows error like "-Xmx1024m: command not found".

After quoting the assignment in double or single quotes, the error disappear.

And I found that the error is caused by the first dash sign. Because all assignments like "a=-bb xx yy ..." will raise the same error: it tried to run the xx as a program. It seems that shell recognize the second word as a command if the first word begins with a dash sign as long as there's no quotes in the assignment expression.

So my question is: what does assignment expression begins with dash sign mean in shell? I'm using bash as default.

3
  • Sorry, you don't understand at all. The dash has nothing to do with it, it's the embedded spaces that delimit the assgnment and make the rest of the line a command. VAR=foo bar will assign "foo" to the variable VAR and then run the command bar with that (all in a subprocess, so the variable does not affect the main shell).
    – MAP
    Jan 17 '17 at 2:38
  • @efan when you found that the error is because of the dash, did you try to falsify it by testing a=bb xx yy?
    – muru
    Jan 17 '17 at 4:18
  • @muru Yes, I understand now, thanks for your comment.
    – efan
    Jan 17 '17 at 6:32
1

First of all, you really should be quoting your variables:

JAVA_OPTS="-Xms1024m -Xmx1024m"

Because the variable was unquoted,in your original command only JAVA_OPTS=-Xms1024m was interpreted as variable assignment,and -Xmx1024m as a command name. This is often refereed to as setting up environment variable for command. For instance,

LC_ALL=C  date +%s

would modify date's environment with setting LC_ALL=C ( that is running date with english locale, even if you have something else set). Note the difference here - LC_ALL=C doesn't have spaces, so word splitting doesn't occur, which is what's happening in case of JAVA_OPTS=-Xms1024m -Xmx1024m

Second, the dash signifies short style of options. There's also long style, for instance bash --posix or ls --format=long. Why dash ? Historical convention. It should be noted that POSIX standard requires only short version of the command-line arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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