一、有时候只是简单的获取下时间戳,不想在python解释器的交互模式下再去执行python代码,如何实现呢?

以循环输出多行为例:
(1)第一种方式:
python -c "exec(\"import sys\nfor r in range(3): print 'arun'\")"
arun
arun
arun
(2)第二种方式:
(echo "import sys" ; echo "for r in range(3): print 'arun'") | python
arun
arun
arun

二、实践

showtime () {
(
echo "import datetime"
echo 'print str(datetime.datetime.now()).split(".")[0].replace("-","").replace(":","").replace(" ","")'
) | python
}

效果如下:

showtime
20190302100502

  

04-14 22:12