Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I run the cd - command without echoing out any output?
I tried cd - 2>&1 /dev/null but that prints out the destination directory.
The reason for this is that I would like to use it an a bash function and I would prefer to not have the unnecessary output.

share|improve this question

4 Answers 4

up vote 8 down vote accepted

Try it like this:

cd - > /dev/null

You can also do:

cd - > uselessfile

This will send the output to the "uselessfile" file instead of to STDOUT.

You can also append instead of creating or overwriting the file.

cd - >> uselessfile
share|improve this answer

@erewok already answered the question, but I will take a stab at explaining what is actually happening and why you are seeing the behavior that you do.

The problem is with your redirection. You are writing:

cd - 2>&1 /dev/null

which because redirections can appear anywhere within the command line (you can confirm this yourself with something simple like echo a b 2>&1 c </etc/fstab d), after redirections are stripped actually becomes:

cd - /dev/null

See the problem? You are passing two arguments to cd, not one argument and two redirections. Since you can only ever be in one current directory at one time, cd only considers its first argument, which in this case is the -.

What you probably intended to write is a cd command with redirection of both stderr and stdout to /dev/null. There are two ways to do that.

The easiest in your case would be to simply inform the shell that you want redirection, by adding a > character. This is what @erewok is suggesting.

cd - 2>&1 >/dev/null

This first redirects stderr (file descriptor 2) into file descriptor 1 (stdout), then redirects stdout (implicit 1) into /dev/null. Note that the ordering is important; you won't get the same result if you reverse the redirections, though in some cases it may appear that you do because of how the particular program uses the respective output streams.

Alternatively, as has been suggested by @demure, you can use &> to redirect both streams at once if this syntax is supported by your shell.

cd - &>/dev/null

Do note that any of these alternatives will also silence the error when $OLDPWD no longer exists, since you are redirecting standard error output into the system black hole. If this is a concern, I would check $? afterwards; you should confirm this yourself if it is a real concern, but at least in my tests, a failed cd (whether cd $someplace or cd -) sets $? = 1 while after success $? = 0.

share|improve this answer

Redirect stdout and stderr: cd - &>/dev/null

the &> is the short hand version of cd - 2>/dev/null 1>&2, which I what I think you meant to use...

  • Note &> needs bash >= 4.0.

Ps. most commands can be pointed at locations, so it is unlikely you actually need to cd

share|improve this answer

Maybe you can use

cd $OLDPWD

but in this case you'll need more typing :)

But you'll have an advantage: if there is any error the message will not redirect into /dev/null.

share|improve this answer
    
If $OLDPWD is unset, this will drop you in ~. cd - will print an error (and presumably leave you where you already were). –  Michael Kjörling May 10 '13 at 7:54
    
You haven't right. If $OLDPWD is unset (why? maybe the user can unset this), the 'cd -' has same result as "cd $OLDPWD". Check the manual page ('cd -' is equivalent 'cd "$OLDPWD" && pwd'). Or you can test it :) 'OLDPWD="" ; cd -' - see you'll drop into ~ and print a ~ character. –  uzsolt May 10 '13 at 12:34
    
I just tested it. On my system, the sequence cd /tmp bash cd - says bash: cd: OLDPWD not set at the second cd; whereas the sequence cd /tmp bash cd $OLDPWD drops you into ~ (almost certainly because $OLDPWD expands to an empty string). cd $OLDPWD will work fine once you have done at least one cd within the shell in question, but not before then. –  Michael Kjörling May 10 '13 at 20:03
    
Hm, yes, you've right. I've tested with zsh and with zsh doesn't drop error message. –  uzsolt May 11 '13 at 5:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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