Bash Script Array Syntax Error Unexpected
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more bash script syntax error unexpected end of file about Stack Overflow the company Business Learn more about hiring developers or posting ads
Syntax Error Unexpected Shell Script
with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question syntax error unexpected end of file in unix shell script and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The
Linux Syntax Error Unexpected
best answers are voted up and rise to the top `Syntax error: “(” unexpected` when creating an array up vote 1 down vote favorite 1 I have two (Debian) Linux servers. I am creating a shell script. On the first one I create an array thus: #!/bin/bash target_array=( "/home/user/direct/filename -p123 -r" ) That works fine. But when I run this on the other server I get: Syntax error: "(" unexpected As syntax error unexpected array php far as I can tell both servers are the same. Can anyone shed some light on why this doesn't work? If I type it into the terminal directly it is fine?? It would appear that when I run it as sh scriptname.sh I get the error, but if I run it as ./scriptname.sh it seems to be ok. What's the difference? bash shell-script array share|improve this question edited Jan 7 at 23:36 Gilles 369k666681119 asked Jan 7 at 14:42 IGGt 398213 Did you copy-pasted the script between the two server? try cat -v script to see if there are spurious char. –LilloX Jan 7 at 14:50 2 sh is not bash and so running it as sh scriptname.sh is wrong –Eric Renouf Jan 7 at 15:01 add a comment| 3 Answers 3 active oldest votes up vote 6 down vote accepted When you use ./scriptname.sh it executes with /bin/bash as in the first line with #!. But when you use sh scriptname.sh it executes sh, not bash. The sh shell has no syntax to create arrays, but Bash has the syntax you used. share|improve this answer edited Jan 7 at 23:37 Gilles 369k666681119 answered Jan 7 at 15:06 Konstantin Morenko 1986 OK, that makes sense
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company
Bash Function Syntax Error Unexpected
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Sh Array
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, shell script array just like you, helping each other. Join them; it only takes a minute: Sign up I am getting error “array.sh: 3: array.sh: Syntax error: ”(“ unexpected” up vote 3 down vote favorite 2 I have written the http://unix.stackexchange.com/questions/253892/syntax-error-unexpected-when-creating-an-array following code: #!/bin/bash #Simple array array=(1 2 3 4 5) echo ${array[*]} And I am getting error: array.sh: 3: array.sh: Syntax error: "(" unexpected From what I came to know from Google, that this might be due to the fact that Ubuntu is now not taking "#!/bin/bash" by default... but then again I added the line but the error is still coming. Also I have tried by executing bash array.sh but no luck! It http://stackoverflow.com/questions/25222259/i-am-getting-error-array-sh-3-array-sh-syntax-error-unexpected prints blank. My Ubuntu version is: Ubuntu 14.04 arrays bash shell ubuntu share|improve this question edited Aug 9 '14 at 19:46 asked Aug 9 '14 at 19:29 Mistu4u 1,46662352 sh array.sh will generate that error because ordinary bourne shells, such as dash, do not understand arrays. bash array.sh should work fine. I tested your script and, under bash, it works for me. –John1024 Aug 9 '14 at 19:43 @John1024, bash array.sh prints blank in my case. –Mistu4u Aug 9 '14 at 19:45 What does echo $BASH_VERSION say? You may have an old version of bash. –Ned Deily Aug 9 '14 at 19:46 @NedDeily, 4.3.8(1)-release –Mistu4u Aug 9 '14 at 19:47 1 Add set -x after the shebang line. –Ned Deily Aug 9 '14 at 19:57 | show 5 more comments 3 Answers 3 active oldest votes up vote 9 down vote accepted Given that script: #!/bin/bash #Simple array array=(1 2 3 4 5) echo ${array[*]} and assuming: It's in a file in your current directory named array.sh; You've done chmod +x array.sh; You have a sufficiently new version of bash installed in /bin/bash (you report that you have 4.3.8, which is certainly new enough); and You execute it correctly then that should work without any problem. If you execute the
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of http://stackoverflow.com/questions/26347650/syntax-error-unexpected-assigning-an-array-in-bash this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question https://viewsby.wordpress.com/2011/11/16/shell-script-arrays-syntax-error-unexpected/ x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up syntax error Syntax error: “(” unexpected assigning an array in bash up vote 0 down vote favorite Within a bash script, I'm trying to pull all files with an extension '.jstd' into an array, loop over that array and carry out some action. My script is failing to copy the path of each script into the array. I have the following script. #!/bin/bash IFS=$'\n' file_list=($(find syntax error unexpected '/var/www' -type f -name "*.jstd")) for i in "${file_list[@]}"; do echo "$i" done echo $file_list unset IFS The line file_list=($(find '/var/www' -type f -name "*.jstd")) works fine in the terminal, but fails in the script with: Syntax error: "(" unexpected I've googled, but failed. All ideas gratefully received. edit: In case it helps in reproduction or clues, I'm running Ubuntu 12.04, with GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu) bash share|improve this question edited Oct 13 '14 at 21:02 Charles Duffy 94.5k15102142 asked Oct 13 '14 at 19:40 JonRed 5681125 2 Don't do this. See mywiki.wooledge.org/DontReadLinesWithFor for reasons why. Do this correctly mywiki.wooledge.org/BashFAQ/001 . Also mapfile might be useful here for bash 4+. –Etan Reisner Oct 13 '14 at 19:47 3 That error indicates that you aren't invoking your script with bash. Are you perhaps running sh yourscript? If so, the manual use of sh will override the #!/bin/bash shebang. (Also, allow me to strongly echo the advice given by @EtanReisner; reading from find into an array in this manner is a very lossy process). –Charles Duffy Oct 13 '14 at 20:02 1 ...also, echo $file_list do
I am using arrays in a script I got the error message as "Shell script arrays Syntax error: "(" unexpected". $ sh arrays.sh #In arrays.sh script I defined some arrays trying to extract values form it The out put I got after running the script is below Shell script arrays Syntax error: "(" unexpected I am struggled a bit to resolve this issue, and the solution I got to know is, most probably in your OS the bash version is updated. So it is unable to resolve the array assignment. To fix the issue use bash instead of using sh while executing the script, that should not throw any syntax error. Example: Run arrays.sh as $ bash arrays.sh -Sany Rate this:Share this:ShareTweetPocketShare on TumblrEmailPrintLike this:Like Loading... Related By Sandeep Posted in bash script, Linux, Shell Scripting 8 Post navigation ← Grep tab inLinux Bash/Shell Script Arrays → 8 comments on “Shell script arrays Syntax error: "("unexpected” Vincent Blouin May 9, 2012 @ 6:36 AM oh my thanks ! Reply Daniel May 22, 2012 @ 9:03 PM Thanks😀 Reply rox January 25, 2013 @ 5:18 PM Merci Reply Vinay February 21, 2013 @ 1:13 PM Thanks ! Reply phuong March 14, 2013 @ 8:54 AM OMG…..thanks very much ^_^ Reply Pingback: pc.casey.jp » 意外な理由(?)で「Shell script arrays Syntax error: “(” unexpected」 jay September 9, 2013 @ 3:32 AM thank you so much .., Reply Harry September 19, 2013 @ 1:19 AM THANKS! Reply Leave a Reply Cancel reply Enter your comment here... Fill in your details below or click an icon to log in: Email (required) (Address never made public) Name (required) Website You are commenting using your WordPress.com account. (LogOut/Change) You are commenting using your Twitter account. (LogOut/Change) You are commenting using your Facebook account. (LogOut/Change) You are commenting using your Google+ account. (LogOut/Change) Cancel Connecting to %s Notify me of new comments via email. Blog Stats 946,938 hits Search Search Follow Blog via Email Enter your email address to follow this blog and receive notifications of new posts by email. Join 59 other followers Recent Posts Quick way to add swap -Ubuntu Get Thread and Hea