How do I run the
cd - command without echoing out any output?
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.
How do I run the
Try it like this:
You can also do:
This will send the output to the "uselessfile" file instead of to STDOUT.
You can also append instead of creating or overwriting the file.
@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:
which because redirections can appear anywhere within the command line (you can confirm this yourself with something simple like
See the problem? You are passing two arguments to
What you probably intended to write is a
The easiest in your case would be to simply inform the shell that you want redirection, by adding a
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
Do note that any of these alternatives will also silence the error when
Redirect stdout and stderr:
Ps. most commands can be pointed at locations, so it is unlikely you actually need to
Maybe you can use
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.