Search code examples
bashdatevariables

Bash Variable cannot work to get current time


I write a very simple script to get the current time and display it, as below:

#!/bin/bash

CurrentTime = "date +%F-%H-%M-%S"
echo "Archive logs at ${CurrentTime}..."

I refer articles like this How to concatenate string variables in Bash

But it does not work, when executing, it will output the following:

./test.sh: line 3: CurrentTime: command not found
Archive logs at ...

Why?


Solution

  • Maybe you want something like this

    CurrentTime=$(date +%F-%H-%M-%S)
    echo "Archive logs at ${CurrentTime}..."
    

    If you surround the equal sign with spaces, then it is likely to be interpreted as an argument for command CurrentTime (that does not exist).