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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With