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
  • 309,379
bsmile
  • 31

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.