Add %20 as part of date format in shell

I am trying to add %20 between the date and time, to return YYY-MM-DD%20HH-MM-SS

How can I escape the % symbol?

date '+%Y-%m-%d%20%H:%M:%S'

Returns:

2023-02-06                   %H:12:40

And when I quote the %20:

date '+%Y-%m-%d\"%20\"%H:%M:%S'

The quotes are also returned:

2023-02-06"%20"11:13:59

>Solution :

The conversion specifier %% expands to a literal %

$ date +%%
%

so

$ date '+%Y-%m-%d%%20%H:%M:%S'
2023-02-06%2011:20:17

However, as this implies you are using the string in a URL, I would recommend simply outputting a space, and letting whatever is constructing the URL ensure that necessary characters are escaped.

Leave a Reply