Bash shell script error sh: [: missing `]'

bash, bitbucket, git, shell, unix

Solution

I've got that kind of error because I didn't use space before ] ex:

if [ "$#" -ne 2]; then

instead of

if [ "$#" -ne 2 ]; then

PS: I know this is an old question but it might do some help for someone :)

Problem

Aim: I'm trying to create a useful shortcut for initializing a git repo locally, and simultaneously creating a remote repo origin on bitbucket or github. This function is added to my `.bashrc` file in my home directory. Here is the bash function (which yields an error): ``` function initg() { # Defaults to bitbucket API local API="https://api.bitbucket.org/1.0/repositories/" local DATA="name=$3&description=$4&is_private=true&scm=git" local REMOTE="ssh://git@bitbucket.org/$2/$3" # Use github if specified if [ "$1" == "gh" ]; then API="https://api.github.com/user/repos" DATA='{"name":"$3", "description": "$4"}' REMOTE="git@github.com:$2/$3" fi if [ -z "$API" ] || [ -z "$DATA" ] || [ -z "$REMOTE" ] || [ -z "$2" ] then echo "A parameter is missing or incorrect" else curl -X POST -u $2 ${API} -d ${DATA} git init git add . git commit -m "Created repo" git remote add origin $REMOTE git push -u origin master fi } ``` The error: ``` username@COMPUTER-NAME /path/to/local/repo $ initg gh username bash_test desc sh: [: missing `]' sh: [: missing `]' Enter host password for user 'username': ``` My Question: First, where is my error? Secondly, how might I improve the control flow or structure of this script to achieve the stated goals?

Original source