Q: Running multiple OS-executable commands in a cell?

Hi all,

I’m new to this site, and to Jupyter/IPython, and since this might be more of a question about Python, I’m not sure I’m in the right place. But work colleagues suggested this is a good place to start with technical questions, so I’m happy for any tips or suggestions you might have for me.

I’m trying to allow my notebook environment to run executable scripts in my PATH in cells. This is easy enough when I have a single command (eg, ‘df -h .’) in a cell, and even works fine with multiple commands on a single line (eg, ‘df -h . ; pwd’),… but doesn’t work if I use two lines, eg:

df -h .

These are all being made available via aliases (%rehashx, or direct via %alias). Any ideas on how to do this? I have a few leads, but figured I’d ask for expert advice first. My guess is that this might have to do with how the parser for line vs cell magic happens, but I’m unsure.

For more information, the use-case here is to allow the Jupyter commands to exactly mirror the standard Linux shell commands, so people learning how to run the software can transfer their skills seamlessly. This is why I’m trying to avoid adding things like the ‘%%bash’ magic, and other common ways of doing this sort of thing.

Thanks very much!

Okay, then why not use a bash kernel connected to your notebook? That seems to be what you really want.

You can test out one by using the link below and then launching a new notebook and selecting bash form the pull-down menu for the new notebook.


After launching that new notebook, see if that is closer to what you want for your described use.
The repo describing the kernel tells you how you’d install it, see here.

In case your software and stuff isn’t private and you don’t need a ton of compute, you can just use MyBinder.org to launch your users into a notebook with this already set up:
To see how to do this so users would open a notebook with that kernel already hooked to the notebook, here is an example link below:

When it is launched it goes directly to the named notebook and has the bash kernel connected.
That notebook is hosted in this repository if you want to see some of how you set up the environment. If it wasn’t a more complicated environment, I could have just used requirements.txt because bash_kernel gets installed via pip.

1 Like

Hi Fomightez,

Thanks for the reply - I can certainly use a bash kernel, but I guess I’m trying to skip that so users needn’t issue any cell magics or other commands they’re unfamiliar with. Eg, let’s say I want users to execute ‘ls’, and since I want the environment to be the same as their usual shell, I want automagic ON so they can literally just use ‘ls’ in a cell, as opposed to ‘!ls’. (A Bash kernel would make this work directly, but I want the standard Jupyter kernel for other things in the same notebook, and without switching.)

Anyway, this all works fine. They can even do ‘ls ; ls’ if they need to run two commands in a cell. Or, on two separate lines, using the ‘!’ magic lets both execute. But two ‘ls’ commands on two separate lines without the bang fails, which seems odd. Maybe intended behavior, maybe a bug, but I’d love a way around it. The attached picture shows an example.

I’m still new to this, so I’m going to play with custom magics and autocall a bit and see if I can get the behavior I want, and I know this sounds incredibly minor (and it is, since I can do 98% of what I want without this), but I thought I’d try nonetheless.

(Our software is open; I’m happy to give the real example, but this seems to capture all the behavior without needing anything additional.)

Thanks again for your help!

Why not have two notebooks and use them side-by-side in JupyterLab? One is the bash kernel and the other is Jupyter?
Or what about a ployglot notebook? I’ve seen SoS notebook as one. Maybe there are others?