Table of Contents
Python is a high-level language famous for its simplicity, flexibility, and readability. At the same time, Bash is a Unix shell and command language used primarily on Unix and Linux systems. Data analysis, machine learning, web development, and scientific computing tasks are widely performed using python language. On the contrary, the bash is used for the tasks like file manipulation, process management, and system administration.
The python script can be written using IDEs like PyCharm, Spyder, Jupyter Notebook, Visual Studio Code, Sublime Text and IDLE, as the bash is a command language, so the commands are given to the terminal or command prompt.
Though Python and Bash are used for different purposes, python and bash are used together for tasks like running system commands, task automation, and other tasks. The python script can be written in any IDE, and then by using the command line, the bash can call the python script as it becomes difficult for bash to perform tasks like data analysis, file manipulation etc.
For every solution, we need to write a python script and Call the python script through bash. So let’s learn it for each approach below.
Using sys.argv
Create a python file and write a script where two arguments are taken and displayed on the output screen.
1 2 3 4 5 6 7 |
import sys arg1 = sys.argv[1] arg2 = sys.argv[2] print("Argument 1:", arg1) print("Argument 2:", arg2) |
The import sys
imported the sys
module to get the command-line arguments passed to the script. After that, we assigned the first argument to arg1
and the second to arg2
. Then, finally, we printed the values of arg1
and arg2
to the console.
The above script will be saved with the name
my_code.py
(as bash will require to call the script). The values ofarg1
andarg2
will be given to the script through the bash command in the next step.
Call the python script my_code.py
created in the previous code example. You can use the following command to call a Python script from Bash with arguments.
1 2 3 4 |
#!/bin/bash python my_code.py hello world |
1 2 3 4 |
Argument 1: hello Argument 2: world |
The shebang #!/bin/bash
indicates this is a Bash script. Then, the python
command called our Python script my_code.py
. Finally, we passed the arguments hello
and world
.
Using argparse
Create a python file and write a script where two string arguments are taken and displayed on the output screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import argparse #Create argument parser parser = argparse.ArgumentParser(description='My script') #Add arguments parser.add_argument('arg1', type=str, help='Description of arg1') parser.add_argument('arg2', type=str, help='Description of arg2') #Parse arguments args = parser.parse_args() #Access command-line arguments arg1 = args.arg1 arg2 = args.arg2 #Print the arguments print("Argument 1:", arg1) print("Argument 2:", arg2) |
First, import the argparse
module and create an argument parser using argparse.ArgumentParser()
. We then added two required arguments to the parser using add_argument()
. Each argument has a name (arg1
, arg2
), a type (str
), and a help string that describes the argument. After that, we parsed the command-line arguments using parser.parse_args(),
which returns an args
object containing the values of the arguments.
The above script will be saved with the name
my_code.py
(as bash will require to call the script). The values ofarg1
andarg2
will be given to the script through the bash command in the next step.
Call the python script my_code.py
created in the previous code fence. You can use the following command to call a Python script from Bash with arguments.
1 2 3 4 |
#!/bin/bash python my_code.py -a hello -b world |
1 2 3 4 |
Argument 1: hello Argument 2: world |
The shebang #!/bin/bash
indicates this is a Bash script. Then, the python
command called our Python script my_code.py
. Finally, we passed the arguments hello
and world
along with -a
and -b
. The string parameters were passed as the type of arguments were set to str
. The code generates the error if a user tries to enter a number.
Use
python
orpython3
to run themy_code.py
as per the installed python version. For example, to check the version of python, typepython --version
.
In the above examples, two arguments are passed to the python script. You can add more parameters or as allowed by the python script.