Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use variables in commands with backticks? #1292

Closed
PotatoesFall opened this issue Jul 27, 2022 · 6 comments
Closed

How to use variables in commands with backticks? #1292

PotatoesFall opened this issue Jul 27, 2022 · 6 comments

Comments

@PotatoesFall
Copy link

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?

@runeimp
Copy link

runeimp commented Jul 27, 2022

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. 😇

@casey
Copy link
Owner

casey commented Jul 28, 2022

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!

@casey casey closed this as completed Jul 28, 2022
@PotatoesFall
Copy link
Author

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

@melMass
Copy link

melMass commented Mar 20, 2024

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`

@casey
Copy link
Owner

casey commented Mar 20, 2024

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

@pniederlag
Copy link

to bad... still true in 2024 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants