# Perl
echo foo bar baz | perl -pe 's/.*[ \t]//'
If you have to strip trailing spaces first, do it like this:
echo "foo bar baz " | perl -lpe 's/\s*$//;s/.*\s//'
The following was contributed by mr.spuratic in a comment:
echo "foo bar baz " | perl -lane 'print $F[-1]'
# Bash
echo foo bar baz | while read i; do echo ${i##* }; done
or is `bash` is not your default shell:
echo foo bar baz | bash -c 'while read i; do echo ${i##* }; done'
If you have to strip a single trailing space first, do
echo "foo bar baz " | while read i; do i="${i% }"; echo ${i##* }; done
# tr and tail
echo foo bar baz | tr ' ' '\
' | tail -n1
although this will only work for a single line of input, in contrast to the solutions above. Suppressing trailing spaces in this approach:
echo "foo bar baz " | tr ' ' '\
' | grep . | tail -n1