1

I tried the following script to give myself convenience in changing directories to different hard disk partitions. But after its execution, the default location does not change, but if I use command line, location change is successful, as shown below. Can anybody help?

john@X61s:~$ pwd
/home/john
john@X61s:~$ vi chdir 
john@X61s:~$ ./chdir c
here ... 1
john@X61s:~$ pwd
/home/john
john@X61s:~$ cd /media/john/90F8-0AAE/
john@X61s:/media/john/90F8-0AAE$ 

The script chdir is the following

#! /bin/bash
dir=$1
if [[ $dir = 'c' ]]; then
echo 'here ... 1'
cd /media/john/90F8-0AAE/
elif [[ $dir = 'd' ]]; then
  echo 'here ... 2'
  cd /media/john/E5A9-932C
elif [[ $dir = 'e' ]]; then
  echo 'here ... 3'
  cd /media/john/56de0045-aa0e-4b69-8270-44b4ec866fa2/
else
  echo "$dir not defined"
  exit
fi
Rinzwind
  • 293,910
  • 41
  • 570
  • 710
bsmile
  • 31
  • 7
  • The script will do the 'cd' ing but when it is ended... you'll be back where you started the script. See http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script on why. – Rinzwind May 04 '15 at 06:41

1 Answers1

0

The reason for this is that the script runs in a subshell; & then returns to where you were...

Try . ./chdir to make it run in the current shell.

Mark Williams
  • 2,580
  • 13
  • 22