[bash]pushd/podd, () in bash

Following commands are not only for bash, but you must ensure they can use your target system tho.

  • pushd
    Remove the top entry from the directory stack, and cd to the new top directory. When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory. The elements are numbered from 0 starting at the first directory listed with dirs; i.e., popd is equivalent to popd +0.
  • popd
    Save the current directory on the top of the directory stack and then cd to dir. With no arguments, pushd exchanges the top two directories.

We can implement bash script like below:

pushd tmp # go to tmp dir
  # do something
  pushd /path/to/another/working/place
    # next do something
    # keep current directory as `pushd`
  popd
  # here is in 'tmp'
popd
# here is out of 'tmp'

Then, we no longer joy with cd.

BTW, we can run a new shell process as below.

(
  # do something
)
echo $? # Can get the result error code

ref: http://ftp.gnu.org/old-gnu/Manuals/bash-2.05a/html_node/bashref_73.html

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.