0

I am on Windows and using GitBash to run shell scripts in bash that run python scripts.

I'm trying to run python inline in a bash shell script using the answer from How to execute Python inline from a bash shell.

I'm using a specific python environment, and the path is defined with an alias.

This is a file called .pyalias:

alias mypython='C:/users/name/mypath/python.exe test.sh'

This is a file called test.sh:

# misc notes at top, like a docstring
print("Hello")
# real file will instead say myPyScript.py etc.

Here is the problem: This is a file called main_run_all.sh:

# misc notes at top, like a docstring

shopt -s expand_aliases
source ./.pyalias

mypython test.sh

mypython -c print("Hello Again")

When I run sh main_run_all.sh, it prints "Hello" to the console (good, it is successfully running the test.sh script), but then it doesn't run the inline command, returning the following error:

test.sh: line 8: syntax error near unexpected token `('
test.sh: line 8: `mypython -c print("Hello Again")'
1
  • You should just forget about aliases. Use a shell function or a script instead. Commented Sep 1, 2022 at 18:55

1 Answer 1

1

You need to put the python code in quotes so the shell doesn't try to parse it like shell code:

mypython -c 'print("Hello Again")' 
# ..........^....................^

If you get python code that contains both double and single quotes, quoting can be a real pain. That's the point when you use a quoted here-doc:

python <<'END_PYTHON'
print("Hello")
print('World')
END_PYTHON
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.