0

I wrote a text file with some linux shell commands and I tried to make it executable with this

sudo chmod a+x filename

But when I am clicking the file it is opening in the text editor. While checking the properties I found it is not executable at all. How to make it executable.

3
  • There will be an option in the GUI to execute text files. Have a look at the GUI properties and options. Perhaps try right-clicking on the text file to see options.
    – joan
    Commented Oct 22, 2019 at 17:46
  • This is the main problem . Like kali or UBUNTU I can't find such option . I am using raspbian buister. Commented Oct 22, 2019 at 17:48
  • Which GUI? I only use my Pis headless.
    – joan
    Commented Oct 22, 2019 at 17:52

1 Answer 1

3

It is unclear what you mean with textfile in the first version of your question. To run it as program it can be a source file or a script. A source file must be compiled with a compiler like gcc to get an executable binary program. With a script you need an interpreter.

I assume you mainly mean to execute a script. Executing it is only possible with an interpreter like bash, python, perl, awk, sed or any other known interpreter. If you want to make it executable you have to tell the operating system what interpreter it has to use. This is done with the first line in the script, a so called shebang. For a bash script you have to insert as first line

#!/bin/bash

For a python script it could be

#!/usr/bin/python3
or better
#!/usr/bin/env python3

Similar lines are needed for other interpreter.

If you have added the shebang then you must make the script executable as you already have done with:

rpi ~$ sudo chmod a+x filename
rpi ~$ ls -l filename
-rwxr-xr-x 1 pi pi 0 Oct 22 18:26 filename

Notice the three x in the modes. If you want to execute the script from the same directory you are in you have to prefix ./ to avoid unwanted execution:

rpi ~$ ./filename
5
  • An alternative to ./ is to add the directory with the script(s) to your PATH environment.
    – Seamus
    Commented Oct 22, 2019 at 17:35
  • I was not talking about python. I am talking about linux shells. Like making a shutdown command that shutdown the PC after specified minutes and I want to make it clickable. So that I can execute that file with double click.(I am not talking about the default shutdown command) Commented Oct 22, 2019 at 17:43
  • 1
    @SohanArafat Just add the shebang #!/bin/bash to the script, make it executable, as both described in the answer and double click it in the GUI.
    – Ingo
    Commented Oct 22, 2019 at 17:48
  • My god you over described everything. But now I get it. Thanks Commented Oct 22, 2019 at 17:52
  • The #!/bin/bash, even though best practice, is not required if you start from the shell. If it is missing, the GUI however will probably not execute it with bash as interpreter. Commented Oct 22, 2019 at 20:59

Your Answer

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

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