0

What is correct bash syntax for modulus ?

My script has a loop that needs to take some action each 100 repetitions ( such as write to a log ) :

counter = 0
...
divisor = 100
remainder = 0
remainder = counter%=divisor
if $remainder = 0; then ...; else ... ; fi

but I receive error :

counter%=divisor: command not found
4
  • You seem to be having more basic problems. You can't have spaces around = in variable assignments. Commented Mar 12, 2015 at 1:13
  • Your script does not look at all like Bash -- or if it is, it's very bizarre Bash. counter = 0, for example, calls the program counter with the arguments = and 0. Commented Mar 12, 2015 at 1:13
  • does bash include the concept of declaring and initializing a variable ? Commented Mar 12, 2015 at 4:47
  • obLink to shellcheck.net here. Check your script for basic syntax errors before posting, please. Commented Mar 12, 2015 at 8:17

1 Answer 1

3

There are a few different ways to get arithmetic context in bash. I usually use double parens

(( remainder = counter % divisor ))
Sign up to request clarification or add additional context in comments.

2 Comments

if (( counter % divisor )) -eq 0; then ... yields " syntax error near unexpected token '-eq' "
The proper syntax for that is if (( counter % divisor == 0 )); then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.