0

Tried using the answer found here:

How to run 'cd' in shell script and stay there after script finishes?

When I add the 'source' command, the directory is still unchanged after script runs, regardless of whether I execute 'source ' or call the script using an alias coded in cshrc.

Any help is much appreciated!

Community
  • 1
  • 1
gum_shu
  • 1
  • 1
  • 2

2 Answers2

1

As you can see below, make sure your call to cd is not executing within a subshell. If it is, this won't work, source or not.

Script with cd in subshell

#!/bin/bash

( cd /etc ) # thie exec's in a subshell

Output

$ pwd
/home/siegex
$ source ./cdafterend.sh && pwd
/home/siegex

Script with cd not in subshell

#!/bin/bash

cd /etc # no subshell here

Output

$ pwd
/home/siegex
$ source ./cdafterend.sh && pwd
/etc
SiegeX
  • 135,741
  • 24
  • 144
  • 154
0

It was necessary to remove "/bin/" from the cd command within this script, in order for the command to work as intended. Removing this removes the subshell issue for this script. Also, coding "$1" in the ls command was invalid in this context.

gum_shu
  • 1
  • 1
  • 2
  • Why did you have these `/bin` s anyway? The shell finds standard utilities without path specifications, and some commands (like `cd` or `echo`) are built-ins. – Philipp Jan 12 '11 at 17:25
  • Used another script as a pattern for the new one. – gum_shu Jun 16 '11 at 18:18