1

I'm using a simple terminal command that adds a rule to iptables and it executes without problems:

sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT

When I run that command in a bash script, it returns sudo: ./script.sh: command not found

The script looks like this:

#!/bin/bash

sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT
  • 3
    Is script.sh executable? chmod +x script.sh – Thomas Mar 31 at 13:10
  • 1
    Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks! – Marin Leontenko Mar 31 at 13:19
  • 2
    @MarinLeontenko a script is just another file. By that logic, all files would be executable by default. – multithr3at3d Mar 31 at 14:50
5

If a script is not executable, one has to call it with an appropriate shell like /bin/sh or /bin/bash. This will ignore the shebang line and the script will be executed with the calling shell.

sudo /bin/sh ./script.sh

To run the script as inteded by OP you have to give the script execution rights.

chmod +x script.sh
sudo ./script.sh

In this case the script is executed with the shell of the shebang line.

  • I believe that you can also use . <scriptname> to run it in the currently running shell. That is how I executed scripts back in the day on my dialup Unix shell. – trlkly Mar 31 at 20:15

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.