-1

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?

alancc
  • 487
  • 2
  • 24
  • 68
  • 2
    Please paste your script at [shellcheck.net](http://www.shellcheck.net/). – Cyrus Jul 02 '23 at 09:28
  • @Cyrus, Thank you. That is a great tool! – alancc Jul 02 '23 at 10:46
  • 1
    _Why?_ : If you write a command line `A B C D`, the first word (`A`) is taken as a program to be executed, and the remaining words are the parameters to the programm. Hence `CurrentTime = something` tries to run a program named _CurrentTime_ with `=` and `something` as parameters. – user1934428 Jul 03 '23 at 10:53

1 Answers1

2

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).

Taige Wang
  • 128
  • 8