MCPcopy Index your code
hub / github.com/ipython/ipython / sx

Method sx

IPython/core/magics/osm.py:681–732  ·  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

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

Callers

nothing calls this directly

Calls 3

parse_optionsMethod · 0.80
getMethod · 0.80
getoutputMethod · 0.45

Tested by

no test coverage detected