pure-bash-bible/manuscript/chapter3.txt

96 lines
1.4 KiB
Plaintext
Raw Normal View History

# Loops
## Loop over a range of numbers
Don't use `seq`.
```shell
# Loop from 0-100 (no variable support).
for i in {0..100}; do
printf '%s\n' "$i"
done
```
## Loop over a variable range of numbers
Don't use `seq`.
```shell
# Loop from 0-VAR.
VAR=50
for ((i=0;i<=VAR;i++)); do
printf '%s\n' "$i"
done
```
## Loop over an array
```shell
arr=(apples oranges tomatoes)
# Just elements.
for element in "${arr[@]}"; do
printf '%s\n' "$element"
done
```
## Loop over an array with an index
```shell
arr=(apples oranges tomatoes)
# Elements and index.
for i in "${!arr[@]}"; do
printf '%s\n' "${arr[$i]}"
done
# Alternative method.
for ((i=0;i<${#arr[@]};i++)); do
printf '%s\n' "${arr[$i]}"
done
```
## Loop over the contents of a file
```shell
while read -r line; do
printf '%s\n' "$line"
done < "file"
```
## Loop over files and directories
Dont use `ls`.
```shell
# Greedy example.
for file in *; do
printf '%s\n' "$file"
done
# PNG files in dir.
for file in ~/Pictures/*.png; do
printf '%s\n' "$file"
done
# Iterate over directories.
for dir in ~/Downloads/*/; do
printf '%s\n' "$dir"
done
# Brace Expansion.
for file in /path/to/parentdir/{file1,file2,subdir/file3}; do
printf '%s\n' "$file"
done
# Iterate recursively.
shopt -s globstar
for file in ~/Pictures/**/*; do
printf '%s\n' "$file"
done
shopt -u globstar
```
<!-- CHAPTER END -->