Wednesday, February 7, 2024

[SOLVED] cd into directory using multiple variables from back script

Issue

This script appears useless I know but I'm testing for a final variant. When I call cd using multiple variables. Nothing happens. However, when I echo ${this}${that} it produces the proper text. What do I need to do to drop into the directory correctly from a bash script?

#!/bin/bash

this=/path/to
that=/final/directory

echo ${this}${that}
cd ${this}${that}

Solution

Add a echo $PWD after the cd, and you'll see that your cd does work. But the script runs in its own shell, and when that shell exits, you're back where you came from.

So you can cd somewhere and do work there as long as you're in the same script. Any program called from your script after the cd will also run in the directory you cd'd to.



Answered By - Robert
Answer Checked By - Mildred Charles (WPSolving Admin)