Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"let" internal shell command doesn't work in a shell script?

Tags:

shell

unix

I did

a=1234 
let "a=a+1"

on command line and it's fine. But when I do the same in a shell script. It prints out an error that "let: not found". Here is the script file.

#!/bin/sh
a=1234;
let "a=a+1";
echo "$a";

Thanks,

like image 569
John Avatar asked Jun 20 '11 10:06

John


4 Answers

Do not use let. Use POSIX arithmetic expansion: a=$(($a+1)). This is guaranteed to work in any POSIX-compliant shell.

like image 55
Erwan Legrand Avatar answered Dec 04 '22 20:12

Erwan Legrand


The problem is likely that /bin/sh is not the same as, or does not behave the same as, your normal shell. For example, when bash is invoked as /bin/sh, it provides a subset of its normal features.

So, you may need to change your shebang line to use a different shell:

#!/bin/bash

or

#!/bin/ksh

You don't need the semi-colons at the ends of the lines.

like image 41
Jonathan Leffler Avatar answered Dec 04 '22 20:12

Jonathan Leffler


See at: http://www.hlevkin.com/Shell_progr/hellobash.htm

The correct is:

a=1234;
b=1;
a=`expr $a + $b`;
like image 32
Edson Matos Avatar answered Dec 04 '22 20:12

Edson Matos


You should use let a=a+1 without quotes

like image 23
mitesh Avatar answered Dec 04 '22 19:12

mitesh