142

Each time I use pushd or popd, it print the stack to standard output. How not to do so?

I don't want to do pushd > /dev/null each time because I have a lot of scripts calling each other.

Maybe a nice override will do it, but I'll need to override these builtins only in my scripts, and then restore the correct behavior.

Cœur
  • 37,241
  • 25
  • 195
  • 267
bemug
  • 1,694
  • 2
  • 12
  • 10
  • 6
    If someone came across this question wanting to silence a single occurrence you can replace `pushd` and `popd` with `cd` and `cd ~-`. `~-` is a _tilde expansion_ that sets to _$OLDPWD_. – Agustín Lado Sep 30 '19 at 19:35
  • @AgustínLado `cd -` also works. Not sure which version is more compatible with wide range of systems. – Shiplu Mokaddim Jan 27 '20 at 14:04
  • 2
    @ShipluMokaddim: Without tilde it prints the folder name (at least on Ubuntu 18.04 running on WSL). – astraujums Feb 05 '20 at 17:39
  • 5
    If you have a sequence of commands between a `cd some/path` and a `cd -` or `cd "${return_wd}"` or something similar, consider grouping the commands in between into a function and use a subshell like `(cd some/path; your_commands_here)`. The directory navigation back to the old PWD is implied at the end of the subshell. Also works for command substitutions. – Sebastian Simon Aug 23 '20 at 17:46

3 Answers3

192

You could add

pushd () {
    command pushd "$@" > /dev/null
}

popd () {
    command popd "$@" > /dev/null
}

to the top of each script. This is probably the minimum amount of work it will take to solve your problem.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Exactly what I needed ... Thank you! – RonzyFonzy Oct 09 '16 at 19:59
  • If you want to silence errors (e.g. no such directory) too, then you can use `&>` instead of `>` – MattSturgeon Apr 16 '17 at 17:36
  • 7
    Be careful when silencing errors: you could create a hard to diagnose failure at a later date. Make sure you check for a not-OK return code and respond to it accordingly. – David Spillett Oct 16 '19 at 11:21
  • This is the least invasive option. – PuzzledVacuum Jan 26 '20 at 09:08
  • You can add `set -e` to the beginning of both functions and `set +e` to the end or just `set -e` at the beginning of the script to exit the script if an exit code != 0 is encountered. This also works in the shebang, e. g. `#!/bin/bash -e`. Source: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html – xdevs23 Apr 18 '20 at 15:18
  • @xdevs23 I don't recommend the use of `set -e`. See https://mywiki.wooledge.org/BashFAQ/105. – chepner Apr 18 '20 at 20:07
  • @chepner valid points, I think it's okay to add it to pushd and popd though. If those fail the script shall exit or it could do things you might not want it to do. – xdevs23 Apr 18 '20 at 21:31
  • Might be better to do `|| exit 1` or `|| exit $?` instead though, in that case you can't catch the errors, so maybe `|| return $?` is even better? – xdevs23 Apr 18 '20 at 21:35
  • It's up to the caller of the function to decide whether to exit the script or not, just like with the built-in commands. – chepner Apr 19 '20 at 01:28
  • 1
    @xdevs23 `|| exit $?` is equivalent to just `|| exit`. Same for `return`. – Sebastian Simon Aug 23 '20 at 17:51
  • don't really need "$@" for popd - it takes no arguments in bash. – bmacnaughton Sep 06 '20 at 18:37
  • 1
    @bmacnaughton It doesn't have any *required* arguments; it accepts `-n` and a whole family of `-N` and `+N` (for an integer `N`) arguments. – chepner Sep 06 '20 at 18:51
  • good point @chepner. i'd just never used them but they're more likely in a script. – bmacnaughton Sep 06 '20 at 19:11
13

In zsh you can setopt PUSHDSILENT. Put this in your ~/.zshrc.

Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74
4

In your .profile file (what ever it is called in your system) add:

pushd () {
    command pushd "$@" > /dev/null
}

popd () {
    command popd "$@" > /dev/null
}

export pushd popd
bozon
  • 67
  • 1
  • 1