2

Consider the bash function:

func() {
  cd /;
  echo test;
}

Calling it does what you'd expect:

~:>func
test
/:>

But when called from a subshell (eg using backticks or $()) it performs very strangely.

~:>func() { cd /; echo test; }
~:>echo $(func)
test Saving session... ...saving history...truncating history files... ...completed.
~:>

This issue does not appear if cd is not executed in the function:

~:>func() { echo test; }
~:>echo $(func)
test
~:>

This is causing issues with some scripts that rely on certain output behavior, eg CLASSPATH=$(func) where func is complex and requires the use of cd.

What's strange is that this worked fine before. It did not start to happen until recently, but I don't know what would have caused it.

Note: alias cd reports "alias: cd: not found" and which cd returns /usr/bin/cd which is a script that is identical to another Mac which does not have the problem.

  • 1
    Looks like the function runs in its own shell somehow. I can't reproduce this here though. Did you change anything in your shell configuration recently? – nohillside Jan 13 '17 at 21:22
  • Does that come from your ~/.bash_logout? – Mark Setchell Jan 13 '17 at 22:55
  • I haven't changed anything recently, but an OS update may have occurred. – Ed Marty Jan 16 '17 at 15:39
  • I don't have any ~/.bash_logout either – Ed Marty Jan 16 '17 at 15:43
1

There seems to be something odd going on as cd is not defined to behave as you are seeing it. Evidence suggests it's definition is being over-ridden somewhere.

It is possible that your cd command is aliased somewhere. The alias command with no arguments will display a list of aliased commands on most shells. A workaround for an aliased command is to preface the command with a backslash (\cd). That quotes the first letter of the command and defeats any alias processing by the shell.

It is also possible that it is a shell function. declare -f should list the defined functions, at least in bash, and you can see if cd is being defined as a function.

With luck, your which command may support short-cut ways to do these checks. Check out this on-line man page for how one implementation of which can be used to check functions and aliases, then see if your which operates in a similar manner.

  • I tried \cd but it had no effect. Note: alias cd reports "alias: cd: not found" and which cd returns /usr/bin/cd which is a script that is identical to another Mac which does not have the problem. – Ed Marty Jan 16 '17 at 15:45
  • gscd should be built into your shell in most every case. – Greg Tarsa Jan 17 '17 at 14:29
  • huzzah: declare -f cd results in a script that appears to have been installed by Ruby. – Ed Marty Jan 17 '17 at 19:34
  • Ah! Ruby. You must be using rvm to manage your rubies. I believe that implementation requires redefinition of cd. Glad you figured it out! – Greg Tarsa Jan 18 '17 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .