The Stack Overflow podcast is back! Listen to an interview with our new CEO.
2 of 2 edited tags; edited title

Why the remote ls from here doc from ssh does not work within a $()?

I am trying to count the number of files in a remote directory.
I have the following code:

ssh server.com<<RUNTHIS  
 FILE_COUNT=$(ls -AU /foo/bar/test_dir) 
 echo "$FILE_COUNT" > ~/file_count.txt   
RUNTHIS

When I run this I get: ls: /foo/bar/test_dir No such file or directory
But the directory is there. If I do ls /foo/bar/test_dir directly to the server it lists the directory.
Also the following works and saves the contents to the file:

ssh server.com<<RUNTHIS  
 ls -AU /foo/bar/test_dir > ~/file_count.txt
RUNTHIS

What is wrong in the first snippet?