I have one shell script opening a perl script. This perl script should be opened in a terminal. I am able to open the terminal but I’m unable to call a cd to reach the perl script’s location
$PROJECT_DIR = "$PROJECT_DIR";
echo "$PROJECT_DIR" > "$PROJECT_DIR/Testing/buildProductPathHello.txt"
osascript -e 'tell app "Terminal"
do script "pwd"
do script "cd $PROJECT_DIR" in window 1
do script "ls" in window 1
do script "./RunTests.pl" in window 1
end tell'
The variable $PROJECT_DIR contains the path, I am verifying this by writing the path into a file. Ultimately, it’s the command cd $PROJECT_DIR is the one that does not work. Does not do cd on the content of the variable.


PS: this is on a mac with a bash shell
Environment variables are specific to each process, too.
The way you’re invoking
osascript, with a single-quoted string, tells the original instance of bash not to substitute for variable names. It actually sends"cd $PROJECT_DIR"toosascript, which sendscd $PROJECT_DIRto Terminal.But
$PROJECT_DIRis not set in the receiving bash process – the one running inside your Terminal window. You can verify that by adding a line likedo script "set" in window 1ordo script "echo $PROJECT_DIR" in window 1.If you enclose the part of the script with the variable name in double quotes, the original bash process will substitute the value of $PROJECT_DIR instead:
(syntax suggested by CharlesDuffy)