OK, I know that there is a lot like this question but I literally get nothing. The thing i need is a script that cd's to /home/project then runs python3 pyscript.py I created an alias to cd alias name='cd /home/project but when I run a shell script I get an error:

: not found: 3: script: script.sh: name ' [Errorno 2] No such file or directory : not found: 5: script.sh

My sh script is:

#!/bin/bash name python3 pyscript.py

Thanks for your help.

  • why do you need it to cd to the directory? why not just run it straight from there e.g /home/project/pyscript.py – alpha Jul 23 '18 at 11:46
  • Don't we need python3 front of pyscript.py to run it. I changed script to /home/project/pyscript.py (I think you meant this) then it gave: not found 3: not found 4: – AKKaygin Jul 23 '18 at 11:50
  • 1
    @DeclanGallagher We don't know if the script uses relative paths, in which case it would have to run with a particular working directory. – Kusalananda Jul 23 '18 at 12:06

The error in your bash is the unknown command name. I don't know what your intention with this command is, so I can not comment further on it.

To run your Python script with /home/project as the working directory, directly on the command line:

( cd /home/project && python3 pyscript.py )

The command is in parentheses so that the cd does not affect the working directory of the interactive shell session.

As an alias:

alias mypyscript='( cd /home/project && python3 pyscript.py )'

As a shell function:

mypyscript () (
    cd /home/project && python3 pyscript.py

As a shell script:

cd /home/project && python3 pyscript.py

In all of the above, python3 pyscript.py would not be invoked if the cd failed.

  • So I changed lines to cd /home/project && python3 pyscript.py but its still giving me Errorno 2 no such file or directory. I'm 100% sure that path to directory is correct. Also sorry for bothering you with this easy problem. – AKKaygin Jul 23 '18 at 11:57
  • @AKKaygin That's the error that Python gives when it can't find the script file. You should look at the contents of /home/project to make sure that the script file pyscript.py lives there and that you are spelling the name of it correctly. – Kusalananda Jul 23 '18 at 12:03

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.