Bug 12578

Summary: `cd somewhere` changes current working directory in /bin/sh scripts
Product: Base System Reporter: pfeifer <pfeifer>
Component: binAssignee: Sheldon Hearn <sheldonh>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 3.2-RELEASE   
Hardware: Any   
OS: Any   

Description pfeifer 1999-07-09 17:00:01 UTC
`man sh` says

   Command Substitution
     Command substitution allows the output of a command to be substituted in
     place of the command name itself.  Command substitution occurs when the
     command is enclosed as follows:

           $(command)

     or (``backquoted'' version):

           `command`

     The shell expands the command substitution by executing command in a sub-
     shell environment and replacing the command substitution with the stan-
     dard output of the command,

yet a `cd /some/where` actually does change the current working
directory of a shell script it occurs in, i.e., apparently it is
not invoked in a sub-shell.

Debian GNU/Linux 2.1 (with bash), Solaris 2.6, AIX 4.1 and DEC OSF1 4.0
behave as expected, see "How to repeat the problem".

Fix: 

Replace /bin/sh by bash.
How-To-Repeat: Run the following script:

  #!/bin/sh

  cd /tmp
  echo .`cd /`.
  pwd  

The expected output (as obtained on Solaris 2.6, Debian GNU/Linux 2.1, 
AIX 4.1 and DEC OSF1 4.0) is as follows
  ..
  /tmp
while FreeBSD 3.2 incorrectly prints
  ..
  /
Comment 1 niall 1999-07-12 19:37:13 UTC
Sheldon Hearn wrote:

> cd /tmp
> echo .`cd /`.
> pwd
> 
> Any takers?

The patch appended seems to fix this, I'd like someone familiar
with sh to review it though, since this may be symptomatic of
a general problem with command substitution.

> PS: And no, this is not an invitation to chat about the default shell
>     for the base system. :-)

You're hinting it should be /bin/sh for root, right?

Regards,

Niall


*** eval.c~     Mon May 10 16:10:16 1999
--- eval.c      Mon Jul 12 18:27:44 1999
***************
*** 710,715 ****
--- 710,716 ----
            && ((flags & EV_EXIT) == 0 || Tflag))
         || ((flags & EV_BACKCMD) != 0
            && (cmdentry.cmdtype != CMDBUILTIN
+                || cmdentry.u.index == CDCMD
                 || cmdentry.u.index == DOTCMD
                 || cmdentry.u.index == EVALCMD))) {
                jp = makejob(cmd, 1);
Comment 2 Sheldon Hearn freebsd_committer freebsd_triage 1999-07-13 11:17:53 UTC
State Changed
From-To: open->feedback

Could you try Niall Smart's patch to src/bin/sh/eval.c and provide 
feedback? 
Comment 3 Sheldon Hearn freebsd_committer freebsd_triage 1999-07-19 12:02:50 UTC
Responsible Changed
From-To: freebsd-bugs->sheldonh

I'll take this one. 
Comment 4 Sheldon Hearn freebsd_committer freebsd_triage 1999-07-19 12:03:59 UTC
State Changed
From-To: feedback->closed

Committed, thanks!