简体   繁体   中英

strange behaviour : export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo “${JARS[*]}”)

export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo "${JARS[*]}")

If I put this line in a bash_script.sh and do

chmod +x bash_script.sh

and then run

./bash_script.sh

it gives the error.

Syntax error: "(" unexpected (expecting ")")

How ever I am able to run this thing directly from the prompt and get the expected result. as

$ export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo "${JARS[*]}")

I was wondering what is the reason for this strange behaviour.

#!/bin/bash添加为第一行,以强制其在bash shell中运行。

Make sure you have #!/bin/bash at the top of your shell script. Array syntax VAR=(...) is a bash-ism. It won't work in plain sh ( #!/bin/sh ).

By the way, that looks like a line from my answer here . If so, I encourage you to use my updated solution rather than this.

There's no need to manually build the classpath list. Java supports a convenient wildcard syntax for directories containing jar files.

 java -cp "$LIB/*" 

(read more)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM