3

I am executing following command.

cd dirname; echo $?

This always return 0 whether of not cd is successful. This is giving me incredible headache.

How to check if cd was successful without reading its error message.

  • Strange. I'm not seeing that behavior on Linux with bash 4.2.39. – Vaughn Cato May 30 '13 at 1:56
  • 1
    I can't reproduce this in bash 4.2.37 on Ubuntu; it will return 1 for me if I try to cd to a nonexistent directory as expected. What version are you using? – j883376 May 30 '13 at 1:56
  • Can't reproduce on bash 4.2.45(1) on Ubuntu. – unxnut May 30 '13 at 1:57
  • 9
    Just to be sure: What is the output of type cd? – Hauke Laging May 30 '13 at 2:00
  • 1
    What version of bash (bash --version) are you using? Also, what operating system are you using? – user26112 May 30 '13 at 2:10
4

The reason may be that cd is not a shell builtin as usual but

  1. a shell function
  2. or an alias

This can be checked with type cd.

(I make the comment an answer so that the question can be "closed".)

2

This problem arose due to some stupidity. As suggested by HaukeLaging, I did type cd. It turned out the cd was aliased to some bash function which was logging the user cd activity on server.

I aliased cd back to cd and the script started working fine. I had the fleeting temptation to delete the question altogether first the I though I should answer it here. I might be useful for someone else.

  • I would suggest checking all aliases on your account. See for example unix.stackexchange.com/questions/8581/… (there is worse) ... also whatever did it accidentally could have done it twice. – babou May 30 '13 at 7:53
  • 3
    Also think about using \cd to override the alias. – terdon May 30 '13 at 15:45
0

One solution is to prefix the call w/ the bash builtin 'command'. This will force the use of the actual binary file 'cd'

    root@ds002:~# command cd /foo
    -bash: cd: /foo: No such file or directory
    root@ds002:~# echo $?
    1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.