doesn't work because cd reads the new directory from the command line argument, not from standard input.
The script doesn't work because to execute it a new instance of bash is started. The new bash changes to the new directory, but the old one doesn't. This can be demonstrated using this script:
The script will tell you it has changed directory, but you will stay in the old dir. To change dir you can use the commandThe quotes are only necessary if your directory names contain spaces or other strange characters. Else using the classical requires less typing.
echo "The old path is:"
echo "Changing dir to $a"
echo "The new path is:"