Home  >  Q&A  >  body text

shell - linux 下sh文件 的 时间输出

在linux 编辑 sh文件

filename=`date +%Y%m%d`

目前只有日期 而没有当天的时间

请问 在后面加上时间 怎么变小呢

filename=`date +%Y%m%d%H`

在这里加上 %H

就会报错 !、

求sh文件编辑 高手 指导一下

filename=`date +%Y%m%d %H:%M:%S`

报错信息如下

date: extra operand ‘%H:%M:%S’
Try 'date --help' for more information.
迷茫迷茫2742 days ago766

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:31:54

    You need to escape the spaces, or make a slight change, wrap the entire formatting parameters in single quotes, and add variable references on the outer layer, so it doesn’t matter how many spaces you put in the middle. The code is as follows

    shellfilename=$(date '+%Y%m%d %H:%M:%S')
    echo $filename
    

    reply
    0
  • PHPz

    PHPz2017-04-17 13:31:54

    filename=date +%Y%m%d%H
    Will this cause an error? What system are you using? I tried it in my centos virtual machine and it worked fine. As for filename=date +%Y%m%d %H:%M:%S, change it to filename=date +%Y%m%d %H:%M:%S; then

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:31:54

    Escape spaces or pass '%H:%M:%S' as the second parameter

    reply
    0
  • Cancelreply