Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushd does not go into specified directory

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.

like image 591
leoOrion Avatar asked Mar 07 '17 07:03

leoOrion


People also ask

Does pushd change directory?

The pushd command saves the current working directory in memory so it can be returned to at any time, pushd moves to the parent directory. The popd command returns to the path at the top of the directory stack. This directory stack is accessed by the command dirs in Unix or Get-Location -stack in Windows PowerShell.

Does pushd create directory?

pushd adds a directory to the top of the stack and popd removes a directory from the top of the stack.

What does pushd in cmd do?

The pushd command is used to save the current directory into a stack and move to a new directory. Furthermore, popd can be used to return back to the previous directory that is on top of the stack. It is very useful when we have to switch between two directories frequently.

What is the pushd command in bash?

To store the current directory information in the stack before moving to another directory location, `pushd` command is used in bash. This command works on LIFO (Last In First Out) based. This means, the directory information will be stored at the end of the stack location.


1 Answers

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
like image 159
Scheff's Cat Avatar answered Sep 20 '22 19:09

Scheff's Cat