user4654 user4654 - 2 months ago 13
Bash Question

.format equivalent in shell script

In

Python
, if I have a to create a string with date as suffix, we can do some thing like this:

date = get_todays_date()
name = "automation_results_{}'.format(date)


What is the equivalent of
.format
in
shell/bash
scripting language?

Answer

In bash you can get a date string by invoking date:

name="automation_results_"$(date +%Y_%m_%d) 

with the different options for the format string you can get any date format you prefer.

As @cdarke commented, if you have a recent version of bash you can also do the faster:

name=$(printf "automation_results_%(%Y_%m_%d)T")