1

I have this script with name example.sh.

echo Write a number, please:
read x
let res=$x+1
echo $res

If i run it this way, I get an error:

mario@gazpatxo-linux:~/temp$ sudo ./example.sh 
[sudo] contraseña para mario: 
Write a number, please:
3
./example.sh: 3: ./example.sh: let: not found

If i run it this other way, I don't get an error:

mario@gazpatxo-linux:~/temp$ sudo su
root@gazpatxo-linux:/home/mario/temp# ./example.sh 
Write a number, please:
4
5

In the second default run I use / bin / bash, which is root's default shell, so it works correctly, but what happens in the first run? What shell am i using? Where can i change it?

Thanks a lot!

  • 1
    You should add a "shebang" to your script. See for example Which shell interpreter runs a script with no hashbang… but run as sudo? – steeldriver Mar 27 at 18:21
  • I get the same as you, which is interesting because all situations show the same default shell in env, being SHELL=/bin/bash. I upvoted your question and also up'd @steeldriver 's comment. – Doug Smythies Mar 27 at 18:49
  • O.K. yes, sudo seems to force sh (if there is not shebang line) which has been pointing to dash instead of bash for several years now. – Doug Smythies Mar 27 at 18:57
0

I tested this as well and if the example.sh is not marked as executable it gave me the same error.

This command should make the file executable.

sudo chmod +x example.sh

Also you should add the shebang #!/bin/bash as steeldriver mentioned in the comments.

| improve this answer | |
0

I finally found an answer to my question.

With the command "dpkg-reconfigure dash" a window opens that indicates that the default dash is associated with "/bin/sh" and allows us to change it to "/bin/bash", so now I can run the second situation without error, since due to the dash configuration, it would be executed with /bin/bash that recognizes the let command.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.