why subtraction return - symbol

前端 未结 2 396
既然无缘
既然无缘 2021-01-28 15:40

I have a problem with a simple subtraction but I don\'t understand what\'s wrong.

My code :

start= date +%s%N | cut -b1-13
#Treatment...
end= date +%s%N         


        
相关标签:
2条回答
  • 2021-01-28 15:54

    The command:

    a= b
    

    (note the space) will set a to an empty string while it runs the command b. It's a way to temporarily set environment variables for a single command, things like:

    PATH=/path/to/somwhere gcc whatever  # Here, PATH has the modified value.
    echo $PATH                           # Here, PATH has its original value.
    

    So the command line:

    start= date +%s%N | cut -b1-13
    

    sets start temporarily to nothing and runs the date command. Hence both start and end are still empty when you use them, which is why you only get the -, since expr - just gives you -.

    If you want to get the results of the date command into a variable, use:

    start=$(date +%s%N | cut -b1-13)
    
    0 讨论(0)
  • 2021-01-28 16:05

    You didn't assign to the variables. You must not have spaces around the equals sign.

    Also, you're doing it wrong.

    start=$(date +%s%N | cut -b1-13)
    
    0 讨论(0)
提交回复
热议问题