I need to make a script do a specific action on first loop iteration.
for file in /path/to/files/*; do
echo "${file}"
done
I want this output:
First run: file1
file2
file3
file4
...
A very common arrangement is to change a variable inside the loop.
noise='First run: '
for file in /path/to/files/*; do
echo "$noise$file"
noise=''
done
You can create an array with the files, then remove and keep the first element from this array, process it separately and then process the remaining elements of the array:
# create array with files
files=(/path/to/files/*)
# get the 1st element of the array
first=${files[0]}
# remove the 1st element of from array
files=("${files[@]:1}")
# process the 1st element
echo First run: "$first"
# process the remaining elements
for file in "${files[@]}"; do
echo "$file"
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With