help-make
[Top][All Lists]
Advanced

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

Re: $(file ...) function and other tools writing to the same file


From: Tim Murphy
Subject: Re: $(file ...) function and other tools writing to the same file
Date: Mon, 14 Mar 2016 13:56:28 +0000

On 14 March 2016 at 13:32, Martin Reinders <address@hidden> wrote:

> Hello,
>
> I am experiencing unexpected results if both the $(file ...) function
> (that was introduced with GNU make version 4) and other command-line
> utilities are used to produce a single file.
>
> Here is a minimal example. (The "echo" command is used for demonstration
> purposes, it stands for any external command-line utility writing to
> standard output.)
>
>   $ cat Makefile
>   all:
>         /bin/echo "LINE FROM ECHO COMMAND" > file.txt
>         $(file >>file.txt,LINE FROM FILE FUNCTION)
>
>   $ make
>   /bin/echo "LINE FROM ECHO COMMAND" > file.txt
>
>   $ cat file.txt
>   LINE FROM ECHO COMMAND
>
> I expected that the echo command writes one line to the file, and the
> $(file ...) function appends another line. But as one can see, the file
> contains the output from the echo command only. It seems to me that the
> echo command is executed _after_ executing the $(file ...) function.
>

Functions and variables are expanded before the command and list of
variables is sent to the shell to be executed.  Hence $(file) happens
before your echo command is executed.

Why would you use $(file) in a commandline anyhow?   The usefulness of
$(file) is as a way, for example, to get around commandline length limits
e.g. when listing hundreds of files to put in a zip  or to create
configuration files for a build in a platform independent way  or things of
that nature.  It's not really there to replace bash commands.

Regards,

Tim


reply via email to

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