[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Possible bug with redirect.

From: Greg Wooledge
Subject: Re: Possible bug with redirect.
Date: Mon, 1 Nov 2021 11:41:08 -0400

On Mon, Nov 01, 2021 at 04:23:32PM +0100, Rikke Rendtorff wrote:
> I'm very new to linux, so I apologize if I'm reporting a non-bug.
> I wanted to do this: date > `date +"%Y-%m-%d"`.txt to put the date into a
> file called 2021-10-16.txt
> But I accidentally forgot the backticks, so it became date > date
> +"%Y-%m-%d".txt. And it created a file called "date" and it put the string
> "2021-10-16.txt" into it.

Before we go any further: backticks are deprecated.  Command substitutions
in new scripts should use the $( ) syntax instead.

> I can understand why the file is called "date", but I got somewhat confused
> about how the content became the '+"%Y-%m-%d".txt' part.

You ran this command:

date +"%Y-%m-%d".txt > date

The redirection ( > date ) can appear at any point in a simple command.
Traditionally, it's written at the end of the command, but in your case,
it appeared in the middle.  All I did was move it to the expected location
to clarify what you did.

So, once the redirection is put aside, you ran the 'date' command with
one argument.  That argument began with a + sign, so date(1) interprets
it as an output format.  The %Y and %m and %d are replaced by the values
from the system clock, and the two hyphens and the .txt are left intact.

The resulting output was 2021-10-16.txt as you saw in the file.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]