Issue
I would like to print git log in bash script line by line, current script output in one line, I guess need to split it by some way but not work!
#!/bin/bash
x=$(git log --oneline --no-merges -n 5)
echo $x
IFS=$'\n' lines=( $(echo $x) )
for line in lines; do
echo $line
done
Solution
git log --oneline --no-merges -n 5 | while IFS= read -r line
do
echo "$line"
done
Assuming that, in reality, you plan to replace that echo with something else (otherwise, anubhava's comment applies: my code is the equivalent of git log | cat
, in which | cat
is a useless convolution.
Or, alternative method,
exec < <(git log --oneline --no-merges -n 5)
while IFS= read -r l
do
echo "$l"
done
Answered By - chrslg Answer Checked By - Gilberto Lyons (WPSolving Admin)