Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

changing and restore working directory in bash

Posted on 2014-03-04
3
Medium Priority
?
2,187 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 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