14

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,

9
  • 4
    You're probably using a different shell - do you have a shebang line at the start of your script ? Commented Jun 20, 2011 at 10:24
  • 1
    Maybe use a=1234 ? Check this: help|grep let too. Commented Jun 20, 2011 at 10:25
  • echo $SHELL - What it says? Commented Jun 20, 2011 at 10:31
  • That first line is invalid. Are you sure you're writing a shell script? Commented Jun 20, 2011 at 10:38
  • 2
    If you're really using unix and not linux, then using #~/bin/sh is getting you the bourne shell. From your command line (when it works with let), type echo $SHELL and use that for your #!/bin/.... Good luck. Commented Jun 20, 2011 at 12:07

8 Answers 8

21

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best solution
10

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.

Comments

4

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

The correct is:

a=1234;
b=1;
a=`expr $a + $b`;

1 Comment

Thanks!!! this worked for me even on bare sh in an alpine container, however - the provided link is broken
2

You should use let a=a+1 without quotes

Comments

1

It's the '$a' of '$a=1234' that is killing you.

The shell does all $ substitutions and THEN evaluates the expression. As a result it saw "=1234" because there was no value to $a.

Use -x to see this.

  bash -x your-script

Comments

0

Check your actual shell with the following command in the command line:

echo $SHELL

It will provide a shell name, use that instead of /bin/sh at the first line of your script.

1 Comment

yes the problem was with the shebang. I changed it to #!/bin/bash (echo $SHELL prints bash) and it works. But what is the purpose of shebang then? I thought it invokes the appropriate shell for executing the script. (I have sh in /bin). Or is it that let is not supported in bourne shell?
0
  1. Check the shell name using

    echo $SHELL

  2. Change the first line of the script accordingly to

    #!/bin/bash

    or

    #!/bin/ksh

    instead of #!/bin/sh.

Comments

-2
c=1
d=1
for i in `ls`
do
    if [ -f $i ]
    then
        echo "$c -> $i"
        c=`expr $c + 1`
    fi
done
c=`expr $c - 1`
echo no. of files $c
for i in `ls`
do
    if [ -d $i ]
    then
        echo "$d -> $i"
        d=`expr $d + 1`
    fi
done
d=`expr $d - 1`
echo no. of direcrories $d

1 Comment

Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain why and/or how it answers the question. Providing this additional context would significantly improve its long-term educational value. Please edit your answer to add explanation, including what limitations and assumptions apply.

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.