2

On writing few commands at command line, it works, However placing the same in a script file throws error. Here is what I am trying to do:

USER=chandan-jay
REPO=test-1
echo "GitHub UserID: $USER"
echo "Repository Name: $REPO"
str={\"name\":\"$REPO\"}
curl_response=($( curl -u "$USER" -H "Content-Type:application/json" https://api.github.com/user/repos -d $str ))
echo ${curl_response[@]}

when executing above code line after line on shell if works file. When putting them together in a .sh file it throws syntax error (line number 13 is curl_response):

__git_create_repo_CLI.sh: 13: git_create_repo_CLI.sh: Syntax error: "(" unexpected__
9
  • Formatting tip: indent each line with four spaces to format a block of code. Commented Jul 21, 2013 at 23:26
  • This code works for me, once I changed the username Commented Jul 21, 2013 at 23:26
  • Are you sure you're running your script using a Bash interpreter? Commented Jul 21, 2013 at 23:26
  • 3
    Do you have #!/bin/bash at the top of your script? If not, it might be defaulting to /bin/sh, which might not support arrays like that. Commented Jul 21, 2013 at 23:27
  • @icktoofay you are right. I tested code with #!/bin/sh and I got error but with #!/bin/bash it works fine. Commented Jul 21, 2013 at 23:30

4 Answers 4

2

Set your interpreter to bash, like so :

#!/bin/bash

Sign up to request clarification or add additional context in comments.

Comments

1

Try using bash instead of sh while executing the code. Have a look at this for a better explaination http://viewsby.wordpress.com/2011/11/16/shell-script-arrays-syntax-error-unexpected/

Comments

1

Does this look like what you want? I just ran the script, unchanged with my username, under bash, which is /bin/sh on Mac OS X and that's what I got.

Comments

-1

Try changing this

curl_response=($( curl -u "$USER" -H "Content-Type:application/json" https://api.github.com/user/repos -d $str ))

to this

curl_response=`curl -u "$USER" -H "Content-Type:application/json" https://api.github.com/user/repos -d $str`

Hope this helps

1 Comment

-1 No. No no no. No backquotes. Also, this now sets the variable to a scalar rather than an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.