Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a script bash to enter multi-line input in jupyter-notebook?

I needed to write a script with configuration environment in only one cell. I want to leave it indented

!(python --version
 which python
 pip --version 
 conda --version 
 which conda) >> config-environment.txt

But cell not acept skip line between each command. How to write? Is it possible write bash script with indented in jupyter-notebook?

like image 406
Bruno Campos Avatar asked Jan 12 '19 17:01

Bruno Campos


People also ask

How do you input multiple lines in a Jupyter notebook?

Jupyter Notebook - Big Data Visualization Tool In order to enter more than one statements in a single input cell, press ctrl+enter after the first line. Subsequently, just pressing enter will go on adding new line in the same cell. To stop entering new lines and running cell, press enter key one more time at the end.

How do you write a multiline bash script?

Using a backslash \ or « to print a bash multiline command If you're writing a multiline command for bash, then you must add a backslash (\) at the end of each line. For multiline comments, you have to use the HereDoc « tag.

How do I type multiple lines in Ipython?

ctrl+o. If you type ctrl+o, you should be able to add additional lines to the input to run them simultaneously. If that doesn't work, as a workaround, you can paste multiple lines after copying them from elsewhere, or you can press enter on a line with a semicolon or unclosed parentheses.


1 Answers

For your particular case, you can simply use semicolon at the end to run it i.e.

!(python --version; \
 which python; \
 pip --version; \
 conda --version; \
 which conda) >> config-environment.txt

For general case, you can use %%bash cell magic command to run the cell in bash i.e.

%%bash script magic

Run cells with bash in a subprocess.

%%bash

(python --version
 which python 
 pip --version 
 conda --version 
 which conda) >> config-environment.txt

You can also have a look at subprocess python module.

like image 61
kHarshit Avatar answered Sep 21 '22 13:09

kHarshit