5

I'm trying this to display a default helptext when no parameters are given when executing the script:

if [[ $@ ]]; then 
    do stuff
else displayHelp; 
fi

displayHelp() {
    echo "some helptext"
}

But for some reason, when executing the script on console, it says:

./myScript.sh: Line 48: displayHelp: Command not found

The same occurs when I call this function via -h parameter

1
  • If you want to check whether any arguments are given at all, it would be more idiomatic to use $# to check their count. Commented Aug 23, 2016 at 14:03

1 Answer 1

19

Functions must be defined before they can be used. So put the method before your call it:

displayHelp() {
    echo "some helptext"
}

if [[ $@ ]]; then 
    do stuff
else displayHelp; 
fi

or put your main code in another method and call this one at the end of your script:

main() {
    if [[ $@ ]]; then 
        do stuff
    else displayHelp; 
    fi
}

displayHelp() {
    echo "some helptext"
}

main "$@"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.