#!/bin/bash python3 print("Hello World") exit() echo "The execution is completed"
Scripts work differently from typing commands directly in a terminal, ie. what you want to do won't work this easily.
The #!/bin/bash
line tells the kernel that the program it's trying to start is a script that need to be run with /bin/bash
. So when you run ./scriptname
the kernel instead runs /bin/bash ./scriptname
and bash will read commands from scriptname
instead of standard input (the terminal).
When bash gets to the python
line it will start the Python interpreter and kindly wait for it exit before continuing to the next line. When Python starts it does the same thing it would when you type python
in the terminal, it will start the interactive interpreter.
Python is unaware that you want to run commands from a script file, and you also can't tell it to skip a few lines.
When Python exits, bash will continue from the next line quit()
which is a syntax error, you also can't tell Bash to skip a few lines after python
.
There are three ways to get Python to run commands:
- Have bash write commands to the input of python via a heredoc (*1). Python can't read user input from the terminal if you do this.
- Use the
-c
option to supply a short list of commands. This is only suitable for extremely short programs that also don't need to use any quotes (for strings). Multiple commands can be separated with semicolons. - Have a separate Python script, this requires a second file. If you understand the security risks associated with temporary files (*2), you can make bash create the file for you if you wish. Just don't create the files in
/tmp
and you should be fine.
The follow up question: Is it possible to get the output Hello world or any other output that is generated in the python interpreter to bash environment and use it in the bash script.
You can use command substitution using $(
and )
, please use these inside double quoted strings. You can even nest double quoted strings on the inside:
test="$(python -c "import sys; print(sys.version)")" echo "Python has finished" echo "test = $test"
And method which you specify to run the python commands will that work for other tools as well?
All three methods listed above will work for many other scripting languages too.
*1 Heredoc example
read -p "How many numbers: " n python <<END for i in range($n): print(i) print("Literal dollar sign \$") END
The <<END
syntax makes the shell (bash) read all lines until it reads the word END
. Note that END
must be at the beginning of the line.
This way you don't need to escape quotes. The shell will still interpret dollar signs, so you can use variables. If you want an actual dollar sign you need to escape it like this \$
.
*2 Example of the dangers of temp files. DO NOT USE THIS CODE
cat >/tmp/python-script <<END for i in range(10): print(i) END python /tmp/python-script
If someone else creates /tmp/python-script
first, this shell script won't even stop if it fails to overwrite the file. A malicious user on the system could make a harmful Python script which will run instead of the intended script.
This is not the only thing that can go wrong.
There are ways to do this safely, but the simplest way would be to create the files in the current working directory or in a dedicated directory in the home directory.
cat >dopythonstuff <<END ... END python dopythonstuff rm dopythonstuff
print "Hello World"
is not validpython3
#!/bin/bash
to#/usr/bin/env python3
and fill the file with regular python code?python3 -c 'print("Hello World")'
?