Printing Array Elements One Per Line
In this step, we'll focus on the main topic of our lab: printing array elements one per line. We'll explore different methods to achieve this in Bash.
Method 1: Using a For Loop
The most straightforward way to print array elements one per line is using a for loop:
- Create a new file called
print_array_loop.sh
:
#!/bin/bash
## Create a sample array
planets=("Mercury" "Venus" "Earth" "Mars" "Jupiter" "Saturn" "Uranus" "Neptune")
echo "Printing planets using a for loop:"
for planet in "${planets[@]}"; do
echo "$planet"
done
- Save the file and make it executable:
chmod +x /home/labex/project/print_array_loop.sh
- Run the script:
./print_array_loop.sh
The output will show each planet on a separate line:
Printing planets using a for loop:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Method 2: Using the printf Command
The printf
command is often more efficient than using a loop with echo:
- Create a new file called
print_array_printf.sh
:
#!/bin/bash
## Create a sample array
planets=("Mercury" "Venus" "Earth" "Mars" "Jupiter" "Saturn" "Uranus" "Neptune")
echo "Printing planets using printf:"
printf "%s\n" "${planets[@]}"
- Save the file and make it executable:
chmod +x /home/labex/project/print_array_printf.sh
- Run the script:
./print_array_printf.sh
The output will be the same as the previous method:
Printing planets using printf:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Method 3: Using the IFS Variable
The Internal Field Separator (IFS) variable can be used to control how array elements are printed:
- Create a new file called
print_array_ifs.sh
:
#!/bin/bash
## Create a sample array
planets=("Mercury" "Venus" "Earth" "Mars" "Jupiter" "Saturn" "Uranus" "Neptune")
echo "Printing planets using IFS:"
## Temporarily change IFS to newline
old_IFS="$IFS"
IFS=$'\n'
echo "${planets[*]}" ## Note: using * instead of @ with IFS
IFS="$old_IFS" ## Restore original IFS
- Save the file and make it executable:
chmod +x /home/labex/project/print_array_ifs.sh
- Run the script:
./print_array_ifs.sh
The output will again show each planet on a separate line:
Printing planets using IFS:
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Method 4: Combining Multiple Techniques
Let's combine these techniques in a more comprehensive example:
- Create a new file called
print_array_combined.sh
:
#!/bin/bash
## Create a sample array
planets=("Mercury" "Venus" "Earth" "Mars" "Jupiter" "Saturn" "Uranus" "Neptune")
echo "Using a for loop with index:"
for i in "${!planets[@]}"; do
echo "Planet $i: ${planets[$i]}"
done
echo -e "\nUsing printf with formatting:"
printf "Planet: %s - %d letters\n" "${planets[@]}" "${#planets[@]}"
echo -e "\nSorted planets:"
printf "%s\n" "${planets[@]}" | sort
- Save the file and make it executable:
chmod +x /home/labex/project/print_array_combined.sh
- Run the script:
./print_array_combined.sh
The output will show different ways to format and print array elements:
Using a for loop with index:
Planet 0: Mercury
Planet 1: Venus
Planet 2: Earth
Planet 3: Mars
Planet 4: Jupiter
Planet 5: Saturn
Planet 6: Uranus
Planet 7: Neptune
Using printf with formatting:
Planet: Mercury - 8 letters
Planet: Venus - 5 letters
Planet: Earth - 5 letters
Planet: Mars - 4 letters
Planet: Jupiter - 7 letters
Planet: Saturn - 6 letters
Planet: Uranus - 7 letters
Planet: Neptune - 7 letters
Sorted planets:
Earth
Jupiter
Mars
Mercury
Neptune
Saturn
Uranus
Venus
Each of these methods has its advantages:
- The for loop is most readable for beginners
- The printf method is more efficient for large arrays
- The IFS method is compact but may be less intuitive
- Combined techniques can provide rich formatting options
Choose the method that best fits your specific use case and coding style.