I need to create and navigate to a directory with one line of command. And it needs to be inside a function. I'm new to terminal and shell scripting and don't know much about it.
My process goes like this:
I write the name of the directory and it is stored to $dirname
variable, and then type "create" and it is stored to another variable named $thecommand
, to create the directory with the name I gave, and navigate to it.
I have a create()
function. Inside the function I did mkdir ./$dirname
, and then cd $dirname
. Then I applied a condition: if what the user wrote in $thecommand
variable (BTW both $dirname
and $thecommand
are read, so the user writes the value) is equal to "create".
The directory was created, but it didn't navigate to it. So I tried creating an alias p="$dirname"
, and applied the alias in the function, still, it created the directory, but didn't navigate!
I would appreciate if someone helped me with this.
Here is my code:
echo "Write Folder name"
read dirname
echo "Write -create- to create the folder"
read thecommand
p="$dirname"
function create() {
mkdir ./"$p"
cd "$p"
}
if [ "$thecommand" == "create" ]
then
create
fi