I have several shell scripts that use pushd and popd extensively. These commands output the paths to stdout which may overwhelm the console output with lots of unwanted text.

Unfortunately, neither pushd nor popd have any silent option according to their man pages. A simple solution is to redirect the stdout to /dev/null.

pushd $PWD > /dev/null

Is it favourable to explicitly redirect to /dev/null everytime I call pushd/popd in the script? Probably not, it’s obnoxius. Let’s 'redefine' the commands in the sript file.

myscript.sh
 1 #!/usr/bin/env bash
 2 
 3 pushd() {
 4     command pushd "$@" > /dev/null
 5 }
 6 
 7 popd() {
 8     command popd "$@" > /dev/null
 9 }
10 
11 # Here comes the code that consumes pushd/popd

Now, whenever pushd/popd is called in the script, their output is redirected to eternal void of /dev/null.

If you think you will never need the output of pushd/popd, you may define these two functions in the .profile file. It however affects the whole system so ponder carefully if several saved lines of code are worth the possible unforeseen consequences.