Bash 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 about Stack Overflow the company Business Learn more about hiring developers or posting ads bash syntax error unexpected token with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack bash syntax error unexpected end of file Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up
Syntax Error Unexpected End Of File Bash Script
Here's how it works: Anybody can ask a question Anybody can answer The 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)
Shell Script Array Syntax Error Unexpected
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 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 syntax error unexpected array php 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 now. As you can probably tell I'm quite new to Linux (currently trying to convert my Powershell scripts shell/bash scripts). I will stick to using ./ from now on. cheers –IGGt Jan 7 at 15:24 That's not necessarily, but there must be coordination between #!/bin/sh and sh script.sh (or #!/bin/bash and bash script.sh) –Konstantin Morenko Jan 7 at 15:26 3 The sh language has no arrays (a[index]=x or a=(x y)). Some sh interpreters support arrays, only ksh88 (not ported to Linux AFAIK) and some
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 Business Learn more about hiring
Syntax Error Unexpected In Shell Script
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question bash function 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 shell script array them; it only takes a minute: Sign up Bash array: Unexpected Syntax error [closed] up vote 18 down vote favorite 2 I am writing this simple bash script as follows. #!/bin/bash array=( /home/abc/Downloads/something.bat /home/abc/Downloads/smb.conf ) echo ${array[@]} I expected it http://unix.stackexchange.com/questions/253892/syntax-error-unexpected-when-creating-an-array to print all the names of the files in the array. But I get this error instead: test.sh: 3: Syntax error: "(" unexpected If I change the declaration of array to array = {/home/abc/Downloads/something.bat /home/abc/Downloads/smb.conf} this error goes away but I still have new errors test.sh: 3: array: not found test.sh: 4: Bad substitution How can I resolve this issue? This is my first time in shell programming so I am unable to fix the issues myself. RESOLVED: I http://stackoverflow.com/questions/11026192/bash-array-unexpected-syntax-error was executing it as sh test.sh but I forgot I had to execute it as bash test.sh arrays bash shell syntax share|improve this question edited Jun 14 '12 at 10:46 Dennis Williamson 167k42235305 asked Jun 14 '12 at 3:17 user1357576 1311211 closed as too localized by Jonathan Leffler, casperOne Jun 14 '12 at 18:58 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question. 1 That's not an associative array. It's a normal, numerically indexed array. –Dennis Williamson Jun 14 '12 at 3:46 @DennisWilliamson Oh Ok. I will edit it –user1357576 Jun 14 '12 at 3:57 1 @user1357576: Post the solution as an answer and mark it accepted, don't edit it into the question. Also, don't edit the title to resolve it, the website will mark the question as solved when you accept an answer. –Daenyth Jun 14 '12 at 4:01 add a comment| 3 Answers 3 active oldest votes up vote 1 down vote Variable assignments can't have a space around the = sign: array=( /a/b/ /c/d ) ^--no spaces ar
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 http://stackoverflow.com/questions/26347650/syntax-error-unexpected-assigning-an-array-in-bash 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 x Dismiss Join the Stack Overflow Community Stack Overflow is https://forums.linuxmint.com/viewtopic.php?t=124226 a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Syntax error: “(” unexpected assigning an array in bash up vote 0 down vote syntax error 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 '/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 syntax error unexpected 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 doesn't make sense for an array. printf '%q\n' "${file_list[@]}" would make sense, sure, but as it is your code only prints the first array entry, and that after string-splitting and glob-expanding it (since you don't quote correctly). –Charles Duffy Oct 13 '14 at 20:04