Solved

changing and restore working directory in bash

Posted on 2014-03-04
3
1,483 Views
Last Modified: 2014-03-04
curr_dir=`pwd`
cd $dir
##
##  Do some processing
##
cd $curr_dir


Is this inefficient way of restoring directory in bash script or is there some other standard way?
0
Comment
Question by:perlperl
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 200 total points
Comment Utility
The variable OLDPWD is filled automatically, so no need for "curr_dir=`pwd`"

cd $dir
##
##  Do some processing
##
cd $OLDPWD

"cd -" will do exactly the same as "cd $OLDPWD && pwd", i.e. it return to the previous working directory and if successful print out its name.

If your shell is "bash" please see also the "pushd", "popd" and "dirs" builtin commands (consult the manpage for "bashbuiltins").
0
 

Author Comment

by:perlperl
Comment Utility
Interesting i will try cd$OLDPWD and cd -  both

Thanks
0
 

Author Closing Comment

by:perlperl
Comment Utility
Thanks,
cd - worked. no need to save unnecessary working dir first
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now