6

I am trying to go into a directory using pushd

 #!/bin/bash

 function cloneAll {
   [ -d ~/mapTrials ] || mkdir ~/mapTrials
   pushd '~/mapTrials/'
   echo $(pwd)
   popd
}

The echo $(pwd) gives me the same working directory that I called the script from.

I read in other SO answers that pushd is only for child processes and that I have to create an alias for it. I have done that also.

I tried doing some commands like mkdir to see where it would be created. It is being created in the directory I called the script from and not the directory specified in pushd.

How do I get this working? How do I get into a specific directory in a shell script and then do commands inside that directory??

Thanks in advance.

leoOrion
  • 1,833
  • 2
  • 26
  • 52
  • If _directory_ `~/mapTrials` does not exist and cannot be created `pushd` may fail. (Possible reasons: missing permissions to create dir. or _file_ `~/mapTrials` already existing) Actually, `pushd` prints an error. However, you can check `$?` for result of last command. It is not 0 (in my case 1) if `pushd` failed. – Scheff's Cat Mar 07 '17 at 08:41
  • In the ancient past, we had such strange effects because we used NFS (in a heterogeneous network with SGI/Irix and PC/Linux servers) which sporadically failed to keep mounted network directories in sync. But this should not be an issue anymore now-a-days. – Scheff's Cat Mar 07 '17 at 08:45
  • @Scheff The first line in the function makes sure the directory always exists – leoOrion Mar 07 '17 at 10:29

1 Answers1

6

I guess I found the error:

pushd '~/MapTrial'

The single quotes (as well as double quotes) prevent the expansion of ~. Move the "snake" out and it should work. E.g.:

pushd ~/'MapTrial'

or

pushd ~/MapTrial
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56