Lance Leroy
Lance Leroy

Reputation: 399

linux command pipe with python "-c" flag

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

Upvotes: 0

Views: 276

Answers (1)

Daweo
Daweo

Reputation: 36725

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

Upvotes: 2

Related Questions