Search code examples
bashfunctionmkdir

How to create a directory in a function


Editing:

I'm trying to build a simple function that print out a name, then creates a directory. I'm new at building function in bash, so I have the below script that didn't work:

dest_path=/home/all/todo    
line="name"

mkdir_for_name() {
echo $1
mkdir $2
}

mkdir_for_name $name
mkdir_for_name $dest_path/$name

What is wrong with that syntax?


Solution

  • Since you are using echo so I believe you want to print directory name; and off course you need to pass 2 arguments to your function. May be call your function in following way. In this way you need not to change your code.

    mkdir_for_name  "$name"  "$dest_path/$name"
    

    Complete script:

    dest_path=/home/all/todo    
    line="name"
    
    mkdir_for_name() {
    echo "$1"
    mkdir "$2"
    }
    
    mkdir_for_name  "$name"  "$dest_path/$name"