I'm working on a project to remotely create a repository in my GitLab CE instance (that part is working!) and then to create a directory using the name of the project (working!) and cd into that directory (here's where I have the issue...) and then to initialize locally and add the remote repository (working!!!)
The issue I'm having is solely changing into the new directory. Whether I use the code shown below, or simply try to cd "$1"
or cd "$*"
I can't seem to get it to work at all!
#!/bin/bash
dir="$*"
wd=$(pwd)
fulldir="$(pwd)/${dir// /\\ }/"
echo "Creating directory $dir"
mkdir -v "$dir"
cd "$dir"
echo "Changing current directory to $dir"
echo $dir
echo $fulldir
The output of this code is:
root@cana:~# ls
glnewproj test
root@cana:~# bash test Hello World
Creating directory Hello World
mkdir: created directory 'Hello World'
Changing current directory to Hello World
Hello World
/root/Hello\ World/
root@cana:~# ls
Hello World glnewproj test
root@cana:~# pwd
/root
How can I cd
into my newly created directory? I'm totally stumped.
Edit:
Typed up the function per ghoti and tested it within .bashrc
and within my test script.
When running the function directly from bash:
root@cana:~# ls
glnewproj test test2
root@cana:~# mkcd "Hello World"
root@cana:~/Hello World#
When running the function from within a new test script:
root@cana:~# ls
glnewproj test test2
root@cana:~# cat test2
#!/bin/bash
mkcd() {
mkdir -p "$1" && cd "$1"
}
mkcd "$1"
root@cana:~# bash test2 "Hello World"
root@cana:~# ls
Hello World glnewproj test test2
So the script is still running as a child and thus not updating the parent shell's current directory. Is my only option to spawn a new shell at the end of the script?