Do commands run from current directory in a shell

2019-07-10 07:27发布

In a bash shell script I tried these two versions:

java -jar abc.jar&

and

CMD="java -jar abc.jar&"
$CMD

The first verison works, and the second version complains that abc.jar cannot be found. Why?

2条回答
仙女界的扛把子
2楼-- · 2019-07-10 07:35

Commands do run from current directory in a shell script.

This is why the first command in your test script worked.

The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.

查看更多
女痞
3楼-- · 2019-07-10 08:00

Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.

What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.

Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.

java -jar /path/to/abc.jar&
查看更多
登录 后发表回答