What does "!" mean at the start of a Jupyter cell command?

I’ve noticed that certain commands in the Jupyter notebooks fail unless you stick a “!” in front of them, and I have no clear intuition why. I’ve tried Googling this to no avail and I haven’t encountered this syntax in Python programming either.

Here’s an example from the Lesson 3 Planets notebook:

The following command:
mkdir -p ~/.kaggle/
results in the following error:
File “”, line 1
mkdir -p ~/.kaggle/
^
SyntaxError: invalid syntax

On the other hand, the modified command succeeds:
! mkdir -p ~/.kaggle/

The same is true for other commands like
! mv kaggle.json ~/.kaggle/
and
! kaggle competitions download -c planet-understanding-the-amazon-from-space -f train-jpg.tar.7z -p {path}

1 Like

It means the code after the ! is not Python but a shell command. It’s an easy way to use these commands in a Jupyter Notebook instead of opening up a terminal and entering the command there.

See https://ipython.readthedocs.io/en/stable/interactive/python-ipython-diff.html#shell-assignment

2 Likes

As stated above, it used to create shell commands so you can interact with your operating system. So for example

! mkdir -p ~/.kaggle/

Would create a new directory. You can see the functionality of popular shell commands here

https://ccrma.stanford.edu/guides/planetccrma/terminal.html

2 Likes

Makes sense, thank you!