将python输出保存到shell变量,类似于p

2024-03-29 13:16:05 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我要做的,但是我想用python代替:

test=$(perl -e 'print "test"')
[webalert@localhost scripts]$ echo $test
test

有没有一个python等价于perl-e?谢谢。在


Tags: testecholocalhostscriptsperlprint等价webalert
1条回答
网友
1楼 · 发布于 2024-03-29 13:16:05
$ test=$(python -c "print 'hello'")
$ echo $test
hello

(作为旁注…) 如果要保留换行符,请在echo中使用引号:

^{pr2}$

最后一个提示:

Perl比Python更适合于一行程序。我倾向于这样做,而不是强迫python成为一种它不是的语言:

$ test=$(python -c "
> import math
> import sys
> 
> for x in sys.argv[1:]:
>    print '2pi R of {}={}'.format(x,float(x)*2*math.pi)
> " 1 2.4 5 6.6)
$ echo "$test"
2pi R of 1=6.28318530718
2pi R of 2.4=15.0796447372
2pi R of 5=31.4159265359
2pi R of 6.6=41.4690230274

相关问题 更多 >