Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash算术表达式';s的副作用未执行_Bash_Post Increment_Arithmetic Expressions_Side Effects - Fatal编程技术网

Bash算术表达式';s的副作用未执行

Bash算术表达式';s的副作用未执行,bash,post-increment,arithmetic-expressions,side-effects,Bash,Post Increment,Arithmetic Expressions,Side Effects,为什么i在第二个for循环中没有递增 (是的,我知道有一个解决办法。)它在由$(命令替换)创建的子shell中递增。当该进程退出时,修改后的值将丢失 以下是看到相同效果的类似方式: $ declare -i i=0 $ for j in {0..2}; do echo "${j} $((i++))"; done 0 0 1 1 2 2 $ for j in {0..2}; do echo "$(echo "${j} $((i++))")"; done 0 3 1 3 2 3 $ i=0 bas

为什么
i
在第二个for循环中没有递增


(是的,我知道有一个解决办法。)

它在由
$(命令替换)
创建的子shell中递增。当该进程退出时,修改后的值将丢失

以下是看到相同效果的类似方式:

$ declare -i i=0
$ for j in {0..2}; do echo "${j} $((i++))"; done
0 0
1 1
2 2
$ for j in {0..2}; do echo "$(echo "${j} $((i++))")"; done
0 3
1 3
2 3
$
i=0
bash-c“let i++”子进程
(设i++)#显式子shell
让我++等待#后台进程

:您正在子shell中执行工作。
$(具有自己环境的子shell)
vs
$(算术表达式))
i=0

bash -c 'let i++'    # Subprocess
( let i++  )         # Explicit subshell
let i++ & wait       # Backgrounded process
: <( let i++ )       # Process substitution
let i++ | cat        # Pipeline

echo "$i"            #  Still 0