0

I've not had any luck in figuring out how to do this:

In ipython, if I execute the following command

var = 'abc'
!echo $var | rev

it works as expected. But, how to capture the output of the shell command back to a variable in python (like new_var = !echo $"var" | rev)?

I've tried most of the examples here Running shell command and capturing the output with no luck, since the variable var isn't working as expected when passed to any of the many solutions using the subprocess library.

horcle_buzz
  • 2,101
  • 3
  • 30
  • 59

1 Answers1

0

This actually worked:

In [99]: out =!echo $var | rev

In [100]: out Out[100]: ['cba']

(the issue was that my shell command was in a for loop where I was reading a file line by line, I didn't expect line breaks there, which was causing the | to screw up the bash command. A simple strip() on each line, - which was my var - before it was passed to the echo fixed that)

horcle_buzz
  • 2,101
  • 3
  • 30
  • 59