How to append the date to a string in bash

15,307

You can use command substitution to accomplish this.
You might also want to familiarize yourself with the date components:

# Save the file name in a variable so we don't repeat ourselves
outfile="/home/pi/backup/backup.zip.$(date +%Y%m%d)"

sudo dd bs=1M if=/dev/sda1 of=/home/pi/backup.img
zip -r "${outfile}" /home/pi/backup.img
cp "${outfile}" ~/backup

The magic here is the $(date +%Y%m%d). This runs date +%Y%m%d and captures the output which will be the current date in YYYYMMDD format.

Share:
15,307
thelearnerofcode
Author by

thelearnerofcode

Updated on July 24, 2022

Comments

  • thelearnerofcode
    thelearnerofcode almost 2 years

    I have a script that backups my Raspberry Pi

    sudo dd bs=1M if=/dev/sda1 of=/home/pi/backup.img
    zip -r /home/pi/backup/backup.zip /home/pi/backup.img
    cp backup.zip ~/backup
    

    I want to know how I can append the date to the backup.zip file, generated by the second line.

    Any tips?