0

This is my script

cd /opt/product/vinoth
-- some operations --
cd -

While executing this command cd - the old path get displayed in the console.

is there is any way to block this from displaying in console.

The same can be avoided in make files by using @ infront of the command.

By surfing i understood that this can be achieved using cmd=$(cd -)

The command was not executing and path was not changed.

0

2 Answers 2

1

Use OLDPWD instead:

/ $ cd /tmp
/tmp $ echo $OLDPWD
/
/tmp $ cd "$OLDPWD"
/ $

From man bash:

   OLDPWD The previous working directory as set by the cd command.
...
cd 
   An argument of - is converted to $OLDPWD before the directory 
   change is attempted.

$(cd -) is run in a subshell, so it cannot affect the current shell.

You can also do:

cd - >/dev/null
1

As explained in this answer, a short variant to not display the directory when cd to the previous working directory is using the tilde expansion ~-:

cd ~-

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