2

I often make snapshots of files using filename.date +%Y%m%D.tar.gz where the date is inserted in the filename, but it does get fiddly some times.

Is there a way to set the date +%Y%m%D expression in a macro which will always be expanded so I can use filename$DATEMACRO.gz.

Can something like that be set as an environment variable etc?

It would save a lot of errors.

vfclists
  • 19,193
  • 21
  • 73
  • 92

4 Answers4

3

The closest I can think of is a shell function:

# With optional support for a date other than today
DATEMACRO () {
    date +%Y%m%d ${1:+--date "$*"}
}
export -f DATEMACRO

To be able to use such a macro permanently from the shell it must be added to .bashrc and exported to the environment eg. export -f DATEMACRO preferably directly after the function definition (as amended in the original answer above), making commands such below executable directly from the shell

cp foo filename$(DATEMACRO).gz

cp bar filename$(DATEMACRO -3 weeks).gz
vfclists
  • 19,193
  • 21
  • 73
  • 92
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I put it in my .bashrc file and it works from the command line, but does not work in a script it doesn't until I include it in the code. Is there some way or some file it can be added to to make it available in all scripts? – vfclists Sep 11 '12 at 18:04
  • 1
    `bash` will allow you to export functions to the environment: `export -f DATEMACRO`. Otherwise, you will have to explicity source a file containing the function definition in your script: for example, `. ~/lib/bash.functions` – chepner Sep 11 '12 at 18:22
3

You need to write a function to get the current date:

#!/bin/bash

DATEMACRO() {
  date +%Y%m%D
}
echo "myfile_$(DATEMACRO)"
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Stephane Rouberol
  • 4,286
  • 19
  • 18
1

You can do the following:

cienas > 23:24 ~ > export D="date +%Y-%m-%d-%H-%M-%S.tar.gz"
cienas > 23:24 ~ > echo `$D`
2012-08-24-23-25-02.tar.gz
cienas > 23:25 ~ > cp .profile `$D`

I added timestamp just to see that it works. Export D in your .basrc file and then you can use $D to get the current date and time. Please note that these quotes around $D are those on tilda key.

Caladan
  • 1,471
  • 11
  • 13
1

There is no way to have a dynamic variables with bash. However, bash has borrowed some innovations originally introduced by ksh so it might one day implement the so called discipline functions ksh93 provides. eg:

#!/bin/ksh93
function DATEMACRO.get
{
    DATEMACRO=$(date +%Y%m%D)
}

$ echo filename$DATEMACRO.gz
filename20120825.gz

ksh93 has several useful scripting features bash is missing, like floating point arithmetic, FPATH, co-processes and also implements pipelines a more logical way so it might be considered as an alternative to bash.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • 'Might one day' is not a huge help in the here and now, I think. – Jonathan Leffler Aug 25 '12 at 01:28
  • @Johnathan Leffler ksh93 is freely available and open sourced. While the question is tagged bash, it is also tagged shell. ksh93 can certainly be considered as an alternative here and now. – jlliagre Aug 25 '12 at 01:39