Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jupyter Notebooks on Google Colab, what's the difference between using % and ! to run a shell command?

Question is in the title.

I know that % usually denotes a "magic variable" in IPython. That's not a concept I'm terribly familiar with yet, but I have read about it.

However, today I saw a tutorial where someone was using it to run a shell command. Normally I have seen and used !.

Is there a difference? Both seem to be doing the same thing when I try them.

like image 614
rocksNwaves Avatar asked Dec 31 '22 05:12

rocksNwaves


1 Answers

The difference is this:

  • When you run a command with !, it directly executes a bash command in a subshell.

  • When you run a command with %, it executes one of the magic commands defined in IPython.

Some of the magic commands defined by IPython deliberately mirror bash commands, but they differ in the implementation details.

For example, running the !cd bash command does not persistently change your directory, because it runs in a temporary subshell. However, running the %cd magic command will persistently change your directory:

!pwd
# /content

!cd sample_data/
!pwd
# /content

%cd sample_data/
!pwd
# /content/sample_data

Read more in IPython: Built-in Magic Commands.

like image 143
jakevdp Avatar answered Jan 13 '23 15:01

jakevdp