微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bash for loop in one line

## 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](https://www.howtouselinux.com/post/bash-for-loop-in-one-line) [Bash For Loop Examples In Linux](https://www.howtouselinux.com/post/bash-for-loop-examples) [What Is Bash in Linux?](https://www.howtouselinux.com/post/what-is-bash-in-linux)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐