I want to store elements of boot command. I tried this :
But it doesn't work. I want to store elements of last reboot command. How can I fix it ?
If you want reboot times you can use awk like this:
# change internal field separator to newline because awk will write each date on new lines
_IFS=$IFS;
IFS=$'\n';
# Grep all lines that starts with reboot, then remove the first 2 fields and print the rest
reboots=($(last | awk '/^reboot/{$1=$2="";print}'));
# Reset IFS
IFS=_IFS;
echo ${reboots[0]}; # Mon Jul 13 10:39
echo ${reboots[1]}; # Mon Jul xx xx:xx
echo ${reboots[2]}; # Mon Jul xx xx:xx
echo ${reboots[...]}; # Mon Jul xx xx:xx
To split a string into an array using IFS as a separator, you can enclose it in parenthesis:
my_array=($(last -F | grep reboot))
echo "${my_array[@]}"