Jump to content
Toggle menu
  • 51 articles
  • 24 files
  • 4 users
  • 750 edits
Tech-Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Bash loops

From Tech-Wiki


Common Bash loop patterns retained as a concise shell reference.

ⓘ
Validation status
Reviewed for shell safety and presentation on 27 September 2026.
!
Be careful with remote or destructive commands
Several examples execute commands over SSH. Test the target list first and do not place destructive operations such as shutdown into an unreviewed loop.

Original technical reference

Executing a Do loop to repeat one command with different hosts

# for num in {1..5}; do ssh host-$num "shutdown"; done
# for host in `cat hosts.txt`; do ssh $host "shutdown"; done
# while read LINE; do COMMAND $LINE; done < FILE

Executing something based on command's output

# for x in $(cmd1) ; do cmd2 $x ; done
# while read x ; do cmd2 $x ; done < <(cmd1)
# while read x ; do cmd $x ; done < file.txt

Executing the same command forever

# while true; do date +%s; done