-1

I am writing a small Bash script to do some automatic commands. In particular, I would like to save the current directory path in a variable called my pwd and then use mypwd to change directory.

I have tried the following lines with no success:

mypwd=${PWD}
cd mypwd/other/
cd $mypwd/other/

How can I perform this operation in Bash?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
alie
  • 63
  • 1
  • 9
  • 1
    Why not use a relative path (either `cd other` or `cd ./other`)? Both these commands will move to a directory relative to the current directory. – Simon Doppler Mar 31 '19 at 10:15
  • Yes correct. Myabe I can reformulate my need as following: save the current path contained in pwd in another variable and use it later from another directory with cd – alie Mar 31 '19 at 10:19
  • `MYPWD` without a dollar sign in front is just static text, exactly like `other`. – tripleee Mar 31 '19 at 10:57
  • You should not use upper case for your private variables; uppercase names are reserved for system variables. – tripleee Mar 31 '19 at 10:58
  • How is `cd $MYPWD/other/` failing? You should quote `"$MYPWD"` but unless the variable contains whitespace or other shell metacharacters, the lack of quoting should be nonfatal. – tripleee Mar 31 '19 at 11:00
  • I do not know why but cd $mypwd/other does not gives error but does not change the directory. Any hint? – alie Mar 31 '19 at 11:09
  • You are probably running it in a script. It works fine but the change will be lost when the script terminates. – tripleee Mar 31 '19 at 12:39
  • @tripleee yes exactly, I am running it from a script. – alie Mar 31 '19 at 12:44

1 Answers1

0

Using relative pathing is the best solution to what you're doing as Simon put it.

An ill-often used, but less recommended solution could be using pushd and popd, which will save the current directory before navigating to a new one. popd can then be used to return to the former directory pushed.

cd /home/user/directory
# Then we use pushd to save the current directory
pushd
cd /home/user/some/other/directory
# ...
# Do work here
# ...
# Then use popd to return to /home/user/directory
popd
# Do more work
Jamie
  • 1
  • 1
  • Welcome to [so]. While your answer seems to be right, could you provide a usefull example to what you're suggesting. – Vulpex Mar 31 '19 at 10:55
  • A portable solution is to `cd` in a subshell; once the shell exits, you will be back where you started. Demo: `pwd; (cd other; pwd); pwd` – tripleee Mar 31 '19 at 10:56
  • Hi jamie, yes the point is the that in reality I would like to save the path into a new variable and use later the variable to come back or do other operations. Thus I would like to save pwd in "mypwd" and the use it for some stuff cd mypwd/dir1 or cd mypwd – alie Mar 31 '19 at 11:11
  • Hello @Vulpex, basically I would like to save the current path given y pwd in a variable and then do " cd variable/dir/" to change directory. – alie Mar 31 '19 at 11:17
  • @alie Please edit your question and specify that. The more specific you are in your question, the better answers you'll get. – Vulpex Mar 31 '19 at 11:21
  • so in practice guys the path appears correct but cd seems to be not working from the script. – alie Mar 31 '19 at 12:18