我有以下几点:
#!/bin/bash
a=0
for d in ./*/ ; do (
cd "$d"
((a++))
echo $a
); done
Run Code Online (Sandbox Code Playgroud)
它进入我路径中的每个目录,递增a
并打印a
。但是,输出始终为 1。这是为什么呢?
来自 bash(1):
Run Code Online (Sandbox Code Playgroud)(list) list is executed in a subshell environment (see COMMAND EXECU? TION ENVIRONMENT below). Variable assignments and builtin com? mands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.
#!/bin/bash
a=0
for d in ./*/
do
((a++))
echo $a
done
Run Code Online (Sandbox Code Playgroud)
(也稍微更传统的格式)
结果是:
1
2
3
4
5
6
7
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22287 次 |
最近记录: |