1) The shell fork off a separate process.
2) That process does exec("/usr/bin/mycommand");
3) That process loads your code, and does cd /opt/mydir
4) It then quits
5) Your shell is still sitting there where it started since each program has its own current directory
cd has to happen inside the shell to work in your shell, nothing else can do it.
Since you're using bash, just put this in your ~/.bashrc instead: