Assign command (with pipe) output to a variable

Hi ,

I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern.

myDate=`ls | grep 2009 | tail -1`
echo "myDate=" $myDate

However, in the presence of the pipe, the code doesn't work.

I appreciate any suggestions.

Thanks

Jeff

What does "doesn't work" mean?
What does happen?

You don't need ls or grep or tail:

set -- *2009*
shift $(( $# - 1 ))
myDate=$1

Works for me

ls
file_1_2007.dat
file_1_2008.dat
file_1_2009.dat
file_3_2009.dat
file_4_2009.dat
file_2_2009.dat

myDate=`ls | grep 2009 | tail -1`

echo "mydate = ${myDate}"

mydate = file_4_2009.dat

Hi,

Thanks for the feedback. I found that it was due to an error in the earlier lines of the code the ls command was not able to find any folders. The logic of the code posted is right.

Thanks again

Jeff