Syntax error: operand expected (error token is +)
Combination of ceving and Tomek's:
#!/bin/bash
read num1 num2 num3
while [ $num1 -lt $num3 ]
do
echo $num1
num1=$((num1+num2))
done
syntax error: operand expected (error token is + ) in bash
The actual error reason is because of uninitialized variable sum
going through the first iteration of the loop. Initialize the variable before entering the loop.
Also a major logical flaw is that you are not even iterating over the input arguments, but just over the counter i
which will produce incorrect results if you pass arguments other than 1 2
from the command-line.
You need to pass over the actual arguments argc
and argv
(arg count and arg vector: for understanding purposes only) and you don't need bc
at all
argc=$#
argv=("$@")
sum=0
for ((i=0; i<${argc}; i++)); do
sum=$((${argv[i]} + $sum))
done
Bash script syntax error: operand expected (error token is =)
bash
variable assignment can not have whitespace around =
. Drop the spaces around =
:
let br=${br}/10
Do the same for all such cases.
Syntax error: operand expected ... in script that was working right
The easiest way to write those assignment is to providean arithmetic context:
((SIZEKB=(SIZE+1023)/1024))
((REQUIREDKB=SIZEKB+MINFREEKB))
You can use also
SIZEKB=$[(SIZE+1023)/1024]
REQUIREDKB=$[SIZEKB+MINFREEKB]
but this is deprecated and provided for compatibility with old scripts (perhaps those written for bash version 1?).
(Linux) syntax error: operand expected (error token is **)
Use $(( ))
to evaluate arithmetic expressions in bash
.
volume=$(($1 * $1 * $1))
You can also use the let
command:
let volume=$1*$1*$1
or you can use (( ))
to execute an arithmetic statement
((volume = $1 * $1 * $1))
What you wrote was being treated as a filename wildcard.
You also need to provide an argument to the script, this will be used as $1
.
$ bash testarith.sh 5
The volume of the cube is 125
Bash script error for syntax operand expected =
tl;dr:
Double-quote your command substitution:
let initialLines="$(grep '' "$1" | wc -l || true)"
That said, as explained in chepner's helpful answer, you could just use a simple assignment without needing to double-quote the RHS, although to get the same outcome as with let
here, be sure to declare the variable with -i
first; e.g., declare -i initialLines
It looks like the RHS of your assignment - the command substitution ($(...)
) - evaluates either to the empty string or a whitespace-prefixed string in the course of the shell expansions that happen before the command is executed, so that bash
ends up trying to parse one of the following commands:
let initialLines=
let initialLines= 2
If you try these in isolation, you'll get the syntax error you've experienced.
The most likely explanation is that you're using BSD wc
, such as on macOS, which outputs the line count requested with -l
with leading whitespace.
A simple fix is to double-quote the command substitution:
let initialLines="$(grep '' "$1" | wc -l || true)"
As for the command in your command substitution (it may just be an example, but it's worth commenting on):
grep ''
will always return all input lines, so it's nothing more than a less efficientcat
.There is no point in using
|| true
:The exit code of the subshell in which the command substitution runs is not relevant, even with
set -e
in effect.Also, by using
true
as the alternative command, you'll break thelet
command as well, because builtintrue
has no stdout output.
In summary, your entire command substitution could be reduced to
wc -l < "$1"
.
Related Topics
"Cannot Write to Log File Pg_Upgrade_Internal.Log" When Upgrading from Postgresql 9.1 to 9.3
Add Suffix to Each Line with Shell Script
Write to a File After Piping Output from Tail -F Through to Grep
Switching Users Using Winscp Between Different Accounts
Sed Permission Denied When Overwriting File
Does Using Xvfb to Run Opengl Effects Version
Analyzing CPU Registers During Kernel Crash Dump
Getmodulehandle(Null) on Linux
Split Delimited File into Smaller Files by Column
Setting the Thread /Proc/Pid/Cmdline
Why Does If [ !$(Grep -Q) ] Not Work When If Grep -Q Does
Pkill Returns 255 in Combination with Another Command via Remote Ssh
Difference Between Linux Kernel and Unix Kernel(Such as Freebsd) from Programmer's Point of View
Argument List Too Long When Concatenating Lots of Files in a Folder