Home > Software design >  linux command pipe with python "-c" flag
linux command pipe with python "-c" flag

Time:05-26

I am trying to do a string printing with python -c flag, e.g. python3 -c "print('Hello World')"

So now I wanna substitute an argument with pipe, e.g. echo "Hello World" | python3 -c "print($1)"

the pipe is to take output from previous command and take it as input to next command, if I am not wrong, this is possible? But I think I got syntax error which I cannot find any source of this

I also bumped into question previously asked, but the solution required python imports and .py file depends on how we run this, I understand but I just wanna get it in a line of command in linux shell

CodePudding user response:

If your input is always single line then you should be able to harness input function for example

echo "Hello World" | python3 -c "print(input().upper())"

would output

HELLO WORLD
  • Related