4

I'm using a PC with Windows 7 and a Python 3.4 Jupyter notebook from Anaconda3 version 2.3.0.

I'm attempting to run the cell magic bash command from a Jupyter notebook and am having problems that I imagine others will encounter as well.

From what I've seen in tutorials, it should be as simple as...

%%bash
pwd

When I attempt this, I get the error "Couldn't find program: 'bash'". This is true for other bash commands. I have also tried !bash and that has not worked.

A few strange observations that may help:

I am able to run bash commands that don't require parameters as long as they are the first line in the cell.

In: 
ls    

Out:
Volume in drive D is DATA
Volume Serial Number is XXXX-XXXX

Directory of D:\...

05/19/2016  06:25 PM    <DIR>          .

However if I put a return above the command, it seems to interpret the cell as python and gives the "name 'ls' is not defined" error.

If I attempt to call the bash line magic command %bash I get the below error:

'ERROR: Line magic function `%bash` not found (But cell magic `%%bash` exists, did you mean that instead?)'.

Thanks in advance for any help.

buruzaemon
  • 3,847
  • 1
  • 23
  • 44
A. Gordon
  • 57
  • 1
  • 1
  • 5
  • 6
    Don't cell magics rely on the underlying operating system? If you are on Windows, then you probably do not have bash. Please see [this possibly related question & answer](http://stackoverflow.com/questions/16281910/ipython-notebook-bash-magic-error) – buruzaemon May 19 '16 at 23:14
  • I having the same on linux. I get `OSError: "sh" shell not found` when I try `!sh`. – BND Apr 24 '19 at 09:50

1 Answers1

6

Did you try what the error message said to try?

i.e. '%%bash' instead of '%bash'

This page seems to indicate that you're going to want two percent signs to utilize the bash script magic.

Edit. Tailored answer to no longer be to specific errawr message. When I run %lsmagic I get the following:

%lsmagic

Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cd %clear  %cls  %colors  %config  %connect_info  %copy  %ddir  %debug  
%dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  
%history  %install_default_config  %install_ext  %install_profiles %killbgscripts  %ldir  
%less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  
%matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2 
%popd  %pprint  %precision  
%profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx 
%reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  
%system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  
%%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  
%%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

The following command works:

%%cmd
dir

buruzaemon nailed it.

wonkybadonk
  • 432
  • 4
  • 14
  • 5
    Hi and thanks for the help. I did try %%bash - that was actually what I tried first given that's how it seems it's supposed to work in the tutorials I've seen. Apologies if I didn't make that very clear above. When I try that I get the error "Couldn't find program: 'bash'" – A. Gordon May 19 '16 at 23:00