MCPcopy Create free account
hub / github.com/ipython/ipython / sx

Method sx

IPython/core/magics/osm.py:682–733  ·  view source on GitHub ↗

Shell execute - run shell command and capture output (!! is short-hand). %sx command IPython will run the given command using commands.getoutput(), and return the result formatted as a list (split on '\\n'). Since the output is _returned_, it will be stored in ipyt

(self, line='', cell=None)

Source from the content-addressed store, hash-verified

680
681 @line_cell_magic
682 def sx(self, line='', cell=None):
683 """Shell execute - run shell command and capture output (!! is short-hand).
684
685 %sx command
686
687 IPython will run the given command using commands.getoutput(), and
688 return the result formatted as a list (split on '\\n'). Since the
689 output is _returned_, it will be stored in ipython's regular output
690 cache Out[N] and in the '_N' automatic variables.
691
692 Notes:
693
694 1) If an input line begins with '!!', then %sx is automatically
695 invoked. That is, while::
696
697 !ls
698
699 causes ipython to simply issue system('ls'), typing::
700
701 !!ls
702
703 is a shorthand equivalent to::
704
705 %sx ls
706
707 2) %sx differs from %sc in that %sx automatically splits into a list,
708 like '%sc -l'. The reason for this is to make it as easy as possible
709 to process line-oriented shell output via further python commands.
710 %sc is meant to provide much finer control, but requires more
711 typing.
712
713 3) Just like %sc -l, this is a list with special attributes:
714 ::
715
716 .l (or .list) : value as list.
717 .n (or .nlstr): value as newline-separated string.
718 .s (or .spstr): value as whitespace-separated string.
719
720 This is very useful when trying to use such lists as arguments to
721 system commands."""
722
723 if cell is None:
724 # line magic
725 return self.shell.getoutput(line)
726 else:
727 opts,args = self.parse_options(line, '', 'out=')
728 output = self.shell.getoutput(cell)
729 out_name = opts.get('out', opts.get('o'))
730 if out_name:
731 self.shell.user_ns[out_name] = output
732 else:
733 return output
734
735 system = line_cell_magic('system')(sx)
736 bang = cell_magic('!')(sx)

Callers

nothing calls this directly

Calls 2

parse_optionsMethod · 0.80
getoutputMethod · 0.45

Tested by

no test coverage detected