Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • 3
    Can't you just use cd - at the end of the function to switch back to the cached $OLDPWD? You don't need to explicitly save it yourself Commented Oct 2, 2020 at 17:23
  • @steeldriver cd - is a good idea, but if the script was sourced and terminated before cd -, the shell would be left in the path that was provided as an argument to the function. Commented Oct 2, 2020 at 17:30
  • You can use trap if you want to restore the start directory (but sourcing such scripts is a general bad idea) Commented Oct 3, 2020 at 15:58