Ubuntu – How to change directory using script

bash

I have this script

#!/bin/bash
cd /home/user/somedir
pwd

it works as expected, but I would like this script to transport me to /home/user/somedir, but I stay in the same dir.

How to write script that will transport me (in gnome-terminal) to /home/user/somedir?

Best Answer

You need to source your script. If not it will be run in a separate subshell, changing the working directory of the subshell but not of the shell you run it in.

To source it :

. myfile.sh

or

source myfile.sh

You can read more here