Solved

changing and restore working directory in bash

Posted on 2014-03-04
3
1,778 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 200 total points
ID: 39904888
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
ID: 39904967
Interesting i will try cd$OLDPWD and cd -  both

Thanks
0
 

Author Closing Comment

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

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

751 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