You must run the assignment and echo test srcipt in the same shell.
root@ijaz-HP-EliteBook-8560p:~# foo=bar
root@ijaz-HP-EliteBook-8560p:~# cat test.sh
#!/bin/bash
echo "$foo"
root@ijaz-HP-EliteBook-8560p:~# . test.sh
bar
this is just one way of doing it.