How to pass a python variables to shell script in azure databricks notebookbles.? How to pass a python variables to shell script in azure databricks notebookbles.? azure azure

How to pass a python variables to shell script in azure databricks notebookbles.?


Per my experience, there are two workaround ways to pass a Python variable to Bash script for your current scenario.

Here is my sample codes using Python3 in notebook.

  1. To pass little data via environment variable in the same shell session of Azure Databricks Notebook, as below.

    %pythonimport osl = ['A', 'B', 'C', 'D']os.environ['LIST'] = ' '.join(l)print(os.getenv('LIST'))%%bashfor i in $LISTdo  echo $idone

It works as the figure below.

enter image description here

  1. To pass large data via file in the current path of Azure Databricks Notebook, as below.

    %pythonwith open('varL.txt', 'w') as f:  for elem in l:    f.write(elem+'\n')  f.close()  %%bashpwdls -lh varL.txtecho '=======show content=========='cat varL.txtecho '=====result of script========'for i in $(cat varL.txt)do  echo $idone

It works as the figure below.

enter image description here