bash - I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected" -


i have written following code:

#!/bin/bash #simple array array=(1 2 3 4 5)  echo ${array[*]} 

and getting error: array.sh: 3: array.sh: syntax error: "(" unexpected

from came know google, might due fact ubuntu not taking "#!/bin/bash" default... again added line error still coming.

also have tried executing bash array.sh no luck! prints blank.

my ubuntu version is: ubuntu 14.04

given script:

#!/bin/bash #simple array array=(1 2 3 4 5)  echo ${array[*]} 

and assuming:

  • it's in file in current directory named array.sh;
  • you've done chmod +x array.sh;
  • you have sufficiently new version of bash installed in /bin/bash (you report have 4.3.8, new enough); and
  • you execute correctly

then should work without problem.

if execute script typing

./array.sh 

the system pay attention #!/bin/bash line , execute script using /bin/bash.

if execute typing like:

sh ./array.sh 

then execute using /bin/sh. on ubuntu, /bin/sh typically symbolic link /bin/dash, bourne-like shell doesn't support arrays. give error message report.

the shell used execute script not affected shell you're using or shell configured login shell in /etc/passwd or equivalent (unless use source or . command).

in own answer, fixed problem using chsh change default login shell /bin/bash. should not have effect. (and /bin/bash default login shell on ubuntu anyway; had changed else previously?)

what must have happened changed command use sh ./array.sh ./array.sh without realizing it.

try running sh ./array.sh , see if same error.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -