I'm writing my first bash script to automate a back up. I'm stuck almost on the first hurdle.
I want use variables to make a file name for a tar archive. I want a filename that looks like hostname date time.tar.bz2
. I can get that on its own using $(hostname)_$(date +"%FT%T")
, but I can't seem to work it into a variable.
My "test script" looks like this:
#! /bin/bash
NAME=$($(hostname)_$(date +"%FT%T"))
tar -cjvf "$(NAME).tar.bz2" testing testing2
(where testing
and testing2
are dummy directories for, well, testing.)
Unfortunately, it does this:
$ scripts/trial.sh
scripts/trial.sh: line 6: NAME: command not found
Thu Jun 10 23:11:06 AEST 2021
I've tried to breakdown the declaration of NAME
as a work around
#! /bin/bash
test of bits and pieces
NAME=$(hostname)_$(date +"%FT%T")
NAME_VAR=$(NAME)
Test=$(date)
echo $NAME_VAR
echo $Test
tar -cjvf "$(NAME_VAR).tar.bz2" testing testing2
Which gets the following in terminal:
$ scripts/trial.sh
scripts/trial.sh: line 6: NAME: command not found
Thu Jun 10 23:34:37 AEST 2021
scripts/trial.sh: line 13: NAME_VAR: command not found
testing/
testing2/
I just cannot see why this should be happening. What am I missing?
-f
). It expects a remote hostname/IP before the colon and a path after the colon. – Freddy Jun 10 '21 at 14:22