50

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.

5 Answers 5

53

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
22

@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.

11

You can use:

cd ~-

According to the bash man page under Tilde Expansion:

If the tilde-prefix is a `~-', the value of the shell variable OLDPWD, if it is set, is substituted.

1
  • Weird but correct answer (for zsh too). Jan 5, 2023 at 0:01
7

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.

4
  • 1
    If $OLDPWD is unset, this will drop you in ~. cd - will print an error (and presumably leave you where you already were).
    – user
    May 10, 2013 at 7:54
  • 1
    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, 2013 at 12:34
  • 1
    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.
    – user
    May 10, 2013 at 20:03
  • Hm, yes, you've right. I've tested with zsh and with zsh doesn't drop error message.
    – uzsolt
    May 11, 2013 at 5:36
3

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

You must log in to answer this question.

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