Shell based time and date formatting - can't add in more than a few string literals

I cobbled this together based on the man page, just skimming it though:

$date +'%m.%d.%Y'\ "%r"\ %z
05.03.2015 12:54:11 PM -0700

This gives:

05.03.2015 12:54:11 PM -0700

which is as close as I can get to what I want:

05.03.2015 - 12:54:11 PM -0700

I can't seem to get the en dash in the middle. I can use a space like I have above if I escape it; looks like a % literal is done by escaping the percent with another percent e.g.: %%

There is a %n for newline, and a very few other modifiers. How do I concatenate the two delimited by a en dash?

And I would love to do this all in the date formatting like I can do in other languages. I really don't want to have to pipe to sed or awk or replace the first space with the " - ".

Answers


It's probably best to (single-) quote the entire format string as one, rather than mixing single-quotes, double-quotes and backslash escaping as you have. I certainly think that's the clearest way to write it:

date +'%m.%d.%Y - %r %z'

Need Your Help

Should I use /* */ or /** */ for the copyright in the top of a java file?

java coding-style comments javadoc copyright-display

There is a copyright comment in each java file, but I don't know which one should I use: /* */ or /** */?

How to save Item template textbox from gridview to database

c# asp.net ado.net

I have a gridview that contains 2 item template 1-checkbox 2-textbox like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.