Solved

changing and restore working directory in bash

Posted on 2014-03-04
3
1,611 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

777 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