乐趣区

关于bash:For-loop-in-bash

10 Bash for Loop In One Line Examples
Bash For Loop Examples In Linux
What Is Bash in Linux?

Bash for Loop In one Line with items

# for i in 1 2 3 4 5 ; do echo "$i" ; done
# for i in {1..5} ; do echo "$i" ; done
# for i in {1..5..1};do echo "$i" ; done
# for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus;   do  echo $planet; done

Bash for loop C style In One Line with items

# for ((i=1;i<=5;i++));do echo $i;done

Bash For Loop In one line with Command Output

# for i in `seq 1 5`;do echo $i ;done
# for i in `cat test`;do dig $i +short ;done
# for i in `awk '{print $1}' test` ;do ping -c 2 $i ;done

Bash For Loop In one Line with variables

# for i in $(cat test);do dig $i +short ;done
# a="a b c"
# for i in $a;do echo $i;done
a
b
c
# a=(a b c)
# for i in ${a[@]};do echo $i;done
a
b
c
# for i in $(seq 1 5);do echo $i ;done

Bash For Infinite Loop In one Line

# for ((; ;)); do echo "Hello World!"; done
# while true; do echo "Hello World!"; done
# while :; do echo "Hello World!"; done

Bash For Loop In One Line with Files

# for i in *; do echo "Found the following file: $i"; done
# for i in `cat filelist.txt`; do echo ${i}; done;
if a line may include spaces better use a while loop:
# cat filelist.txt | while read LINE; do echo "${LINE}"; done

10 Bash for Loop In One Line Examples
Bash For Loop Examples In Linux
What Is Bash in Linux?

退出移动版