Variables value gets lost in subshell

前端 未结 5 1300
忘了有多久
忘了有多久 2020-12-21 09:57

This bash script concatenates the names for jar files to a classpath (variable CP), in the while loop the value is correct but is lost in the subshell as descibed in this re

5条回答
  •  抹茶落季
    2020-12-21 10:29

    The issue here is that using while in a pipeline creates a subshell, and a subshell cannot affect its parent. You can get around this in a few ways. For what you are doing now, this will suffice:

    for JAR in *; do
        # Your stuff
    done
    

    Another thing to note is that you shouldn't rely on parsing ls

    This also shows you ways to avoid the subshell.

提交回复
热议问题