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.
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.
pushd adds a directory to the top of the stack and popd removes a directory from the top of the stack.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With