It is important to understand the following: a child process is born with its own environment and cannot affect the variables of its parent. If you set a variable in a child process, the value of the variable in the parent will not be affected. These are actually two different variables that have the same name.
The second thing you need to understand is when bash runs the command as a child process. There are two questions related to the question:
- Each process associated with the channel
| , is a child of the current shell. - Running a single built-in command with redirection (for example,
< ) will not spawn a child process.
Here is a simple session that demonstrates these ideas:
$ somevar=initial $ echo test1 | read somevar $ echo $somevar initial $ read somevar < <(echo test2) $ echo $somevar test2
The first read is a child process, so somevar in the main shell does not change. The second read is executed by the main shell itself and, therefore, is updated by somevar .
This means that your code will work as you expect, unless you add a pipe before or after the for loop, i.e. this works the way you want:
# DEFINE HERE SOMEVAR=0 DAEMON_COUNT=10 for i in `seq 0 ${DAEMON_COUNT}`; do if [ ! -d "data$i" ]; then
source share