Bash script not recognizing command-line arguments?

3k Views Asked by At

I am calling the bash script mkproj.sh by using the command ./mkproj.sh. I also try calling it with arguments: ./mkproj.sh hello but my script returns a null $1 when I put echo "$1" in the script. I am not sure why it won't recognize the command-line arguments.

check_for_file()
{
if [ $# -eq 0 ]; then
        local testing_file=myproject
else
        local testing_file=$1
fi

if [ -d $testing_file ]; then
        echo "Directory name already exists"
        exit
else
        mkdir -p "$testing_file"/{archive,backups,docs/{html,txt},assets,database,src/{sh,c}}
fi

}
check_for_file


Thank you!

1

There are 1 best solutions below

1
Arkadiusz Drabczyk On

Positional parameters within functions shadow global positional parameters that script received. You need to call your function like that:

check_for_file "$1"