How can I obtain current date in the following format using Bash
YYYY-MM-DDThh:mm:ss
Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...
date +%S. Displays seconds [00-59] date +%N. Displays in Nanoseconds. date +%T.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
With these options:
$ date "+%Y-%m-%d %H:%M:%S" 2013-10-24 10:40:12
or even (thanks Birei!)
$ date "+%F %T" 2013-10-24 10:40:12
All format controls can be get from man date
.
I don't know what your T
means, but for example you have:
$ date "+%F %T %Z" 2013-10-24 10:40:12 CEST
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With