Skip to content

How to use variables in commands with backticks? #1292

Closed

Description

@PotatoesFall

I read the README and couldn't find this. If I missed it, apologies.

In Makefile, I could do this:

GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_TIME := $(shell git show -s --format=%ci $(GIT_COMMIT) | tr -d '\n')

In just, I understand that backticks can be used to save the output of a command:

GIT_COMMIT := `git rev-parse --short HEAD`
GIT_TIME := `git show -s --format=%ci $(GIT_COMMIT) | tr -d '\n'`

however, the second command fails with /usr/bin/sh: GIT_COMMIT: command not found

The {{}} syntax also doesn't work here, how can I use a variable in a command?

Activity

  1. runeimp commented on Jul 27, 2022

    @runeimp

    My guess is that all or multiple lines like that are processed concurrently or at least not in order. Though @casey would know for sure. 😇

  2. casey commented on Jul 28, 2022

    @casey
    Owner

    So a few things are preventing this from working. First, just variables are distinct from shell variables, so you'll need to use the export keyword to export them:

    export GIT_COMMIT := `git rev-parse --short HEAD`
    export GIT_TIME := `git show -s --format=%ci $(GIT_COMMIT) | tr -d '\n'`
    

    However, assignments are not processed in the order that they appear in a source file, so exported variables aren't available in backticks within the same scope, so GIT_COMMIT still wouldn't be available to the backtick assigned to GIT_TIME.

    Hopefully we'll eventually support some kind of syntax that allows this, perhaps #799 or #822, but it might be a while! Sorry I don't have better news!

  3. PotatoesFall commented on Jul 28, 2022

    @PotatoesFall
    Author

    Ah okay that's clear, thanks. I'm sure I'll find another solution for my problem :)

  4. melMass commented on Mar 20, 2024

    @melMass

    Sorry to bump an old issue but just to be sure, this is not possible to escape variables directly either right?

    root := `{{justfile_directory() / ".."}} | path expand`

    (I use nushell here but you can replace the idea with realpath {{justfile_directory() / ".."}

    EDIT: actually in my case the execution pwd in in fact justfile_directory.. so unrelated but this works fine: root := `".." | path expand`

  5. casey commented on Mar 20, 2024

    @casey
    Owner

    That's correct, interpolations in backticks aren't supported.

  6. pniederlag commented on Apr 26, 2024

    @pniederlag

    to bad... still true in 2024 ?

  7. runiq commented on Apr 24, 2026

    @runiq

    You can use the shell() function, which just takes a string, which you can construct the usual way. For example:

    FILE := "/etc/os-release"
    CONTENTS := shell("cat " + FOO)
    
    # Shows the contents of the file whose name is in the variable FILE
    show-file:
        echo {{CONTENTS}}
  8. casey commented on Apr 24, 2026

    @casey
    Owner

    There are a few other ways to make this nicer:

    FILE := "/etc/os-release"
    
    # shell can take arguments:
    CONTENTS := shell("cat", FOO)
    
    # f strings support formatting:
    CONTENTS := shell(f"cat {{FOO}}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions