Arrays in Bash
#!/usr/bin/env bash
# Create the array
myarray=()
# Add items to the array
# Add a sequence of numbers from 1 to 16
#for item in {0..16}
cd ~/Documents/
# Add a list of files to the array
# List all *.txt files in the ~/Documents directory and grab the three oldest
# To grab the 3 most recent files use ls -rt. Order by time in reverse with most recent listed last
for item in $(ls -t *.txt | tail -3)
do
myarray+=("${item}")
done
# Iterate over the array and print out items
for item in ${myarray[@]}
do
printf "Item: ${item}\n"
done
# Print out the number of elements in the array
printf "${#myarray[@]}\n"
Comments
Post a Comment