1

I have the following file:

$ cat my_exec.sh 
#!/usr/bin/env python
print(10)

It should just print 10. However, I can't get it to do so:

$ sudo ./my_exec.sh
sudo: ./my_exec.sh: command not found
$ sh my_exec.sh 
my_exec.sh: line 3: syntax error near unexpected token `10'
my_exec.sh: line 3: `print(10)'

How do I run my file?

asked Mar 10, 2020 at 10:29
3
  • 1
    Rename your script to my_exec.py and update the first line to #!/usr/bin/python Commented Mar 10, 2020 at 10:33
  • 1
    sudo means "super user do". Do you need your script to be executed with elevated/different permissions? If not, don't use sudo. sh executes a shell script in POSIX-compliance mode, so you don't want that either. Have you tried ./my_exec.sh ? Commented Mar 10, 2020 at 10:35
  • I did sudo because simply doing ./my_exec.sh resulted in permission denied. Commented Mar 10, 2020 at 10:38

3 Answers 3

2

You can run it via the python command:

$ python my_exec.sh

To run it as simply ./my_exec.sh, you need to make the file executable first:

$ chmod 755 my_exec.sh

Also note that by convention python files end in .py .

answered Mar 10, 2020 at 10:34
Sign up to request clarification or add additional context in comments.

Comments

2
  • Change the shebang to #!/usr/bin/env python
  • Change the filename to my_exec.py, as is convention for python files
  • You can run with python my_exec.py
  • You can chmod +x my_exec.py and then ./my_exec.py
dspencer
4,5014 gold badges25 silver badges46 bronze badges
answered Mar 10, 2020 at 10:35

Comments

0

You must enter the directory that you have saved you file through the cmd with cd command. After that you just execute the file with : python name_of_the_file.py . But first you must make it executable with chmod command

For example if you have saved your file at Desktop with the name mycode.py :

cd Desktop
chmod +x mycode.py
python mycode.py
answered Mar 10, 2020 at 10:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.