Solved

changing and restore working directory in bash

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
winscp 000webhost.com 6 95
Linux Desktop suggestion for Dell Inspiron 3043 13 63
number in printf 13 42
php56-php-mcrypt for rhel7 php56 1 100
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.
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.

713 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