This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Difference between sh and Bash
(11 answers)
Closed 5 years ago.
When I try to execute the following
#!/bin/sh
folders=("/usr/include")
for i in ${folders[#]}; do
echo ${i}
done
exit 0
I get test.sh: 3: test.sh: Syntax error: "(" unexpected. I also tried using #!/bin/bash but it didn't work neither. I tried this on Debian Jessie in a script and it produced the above mentioned error. The same script runs fine on Ubuntu 14.04.
Additionally I tried
# foo=(/usr/include); echo $foo
/usr/include
which worked well.
I also checked it for non-printable characters with cat -e test.sh
#!/bin/sh$
$
folders=("/usr/include")$
for i in ${folders[#]}; do$
echo ${i}$
done$
exit 0$
Edit
I executed the script as sh ./test.sh
With bash, you should use declare -a :
#!/bin/bash
declare -a folders=("/usr/include");
for i in ${folders[#]}; do
echo ${i}
done
exit 0
Related
This question already has answers here:
bash command not found when setting a variable
(2 answers)
Closed 2 years ago.
Bash in Linux give me command not found error in Line 9 where I do $array[$i]="Name_Of_File$i".
There are also the correct numbers printed besides the error. But i can't figure how it is possible. Maybe some spaces ? On the net i fuond these sintax of writing assignement with an array.
declare -a array;
start=0;
NumFile=$(ls -1 -d log/log_cassiere* | wc -l);
for (( i=$start; i<$NumFile; i++))
do
$array[$i]="Name_Of_File$i";
done
echo ${array[0]};
The problem with:
$array[$i]="Name_Of_File$i"
is that it is actually interpreting $array[$i] because you have a $ preceding it. That means it will attempt to replace the left side of the assignment with the value. Instead, you should have:
array[$i]="Name_Of_File$i"
This question already has answers here:
Bash array: Unexpected Syntax error [closed]
(3 answers)
Closed 6 years ago.
When I run the following code
#!/bin/bash
folder="~/Desktop/"
if [ -d "$folder" ];
then
cd $folder;
listofpdf=$( find *.pdf -type f);
fi
echo ${listofpdf};
for words in $listofpdf
do
array+=("$words")
done
I get "Syntax error: word unexpected (expecting ")")". It seems to be related to the array variable. Appreciate corrections . Thanks in advance
Although your script is a valid bash script, it is not a valid POSIX shell script: arrays are not supported. Even if sh is a link to or copy of bash, it runs in a strict POSIX mode. In order to run a script with bash extensions, it must be run with bash, either by running bash script or (as you did) using #!/bin/bash as the shebang and executing the script directly with ./script
This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Closed 7 years ago.
I have a very simple shell script which I'm using to loop through directories, and call another shell script. I wrote it on my local machine (OS X running Bash 3.2) and am using it on a remote server running Bash 4.2.
On the server, when I type which bash, I get /bin/bash, so I added the line on top. I still get this error, pointing to the line that begins arrIN=...
8: run_all_verification.sh: Syntax error: "(" unexpected (expecting "done")
The shell script:
#!/usr/bin/bash
# Base name for all experiments
BASE_EXP_ID=$1;
for i in ${BASE_EXP_ID}*
do
# Split file name by "__"
arrIN=(${i//__/ });
EXP_ID=${arrIN[0]}
NUM_FEATURES=${arrIN[1]}
echo "${EXP_ID} ${NUM_FEATURES}"
sh run_verification.sh ${EXP_ID} ${NUM_FEATURES}
done
Your error message is from Dash, probably because you ran sh filename.
To run a script with Bash, use bash filename (or ./filename).
I have written the 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 prints blank.
My Ubuntu version is: Ubuntu 14.04
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 script by typing
./array.sh
the system will pay attention to the #!/bin/bash line and execute the script using /bin/bash.
If you execute it by typing something like:
sh ./array.sh
then it will execute it using /bin/sh. On Ubuntu, /bin/sh is typically a symbolic link to /bin/dash, a Bourne-like shell that doesn't support arrays. That will give you exactly the error message that you report.
The shell used to execute a script is not affected by which shell you're currently using or by which shell is configured as your login shell in /etc/passwd or equivalent (unless you use the source or . command).
In your own answer, you say you fixed the problem by using chsh to change your default login shell to /bin/bash. That by itself should not have any effect. (And /bin/bash is the default login shell on Ubuntu anyway; had you changed it to something else previously?)
What must have happened is that you changed the command you use from sh ./array.sh to ./array.sh without realizing it.
Try running sh ./array.sh and see if you get the same error.
Instead of using sh to run the script,
try the following command:
bash ./array.sh
I solved the problem miraculously. In order to solve the issue, I found a link where it was described to be gone by using the following code. After executing them, the issue got resolved.
chsh -s /bin/bash adhikarisubir
grep ^adhikarisubir /etc/passwd
FYI, "adhikarisubir" is my username.
After executing these commands, bash array.sh produced the desired result.
I am having some trouble with a simple shell script on my ubuntu box. Here is the first part of the script:
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
servers=(
199.315.117.225
46.31.151.106
46.31.154.82
)
I run the script like this:
sudo sh script.sh
And this is the output:
script.sh: 6: script.sh: Syntax error: "(" unexpected
As far as I can tell, that is a valid array, so I don't understand why that paren is unexpected. I am a newbie so any help is much appreciated.
The problem is that you're running your script using a shell (namely Dash) that doesn't support a feature your script is using (namely Bash-style arrays).
The easiest fix is to change this:
#!/bin/sh
to this:
#!/bin/bash
so that your script is run using Bash instead of Dash.
/bin/bash is superset of /bin/sh... This array syntax works in bash.
https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh
http://www.linuxjournal.com/content/bash-arrays
In shell script array should be mentioned by
bash:
declare -a varname
varname=( 192.168.1.1 192.168.1.2 192.168.1.2 )
and you can get all ip by
echo "${varname[#]}"
or
echo "${varname[*]}"
you can try the below code,
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
ARRAY="199.315.117.225 46.31.151.106 46.31.154.82"
for i in $ARRAY
do
echo $i
done