just icon indicating copy to clipboard operation
just copied to clipboard

Shell prelude

Open casey opened this issue 1 year ago • 8 comments

Users often have scripts they'd like to source, alias they'd like to define, or environment variables they'd like to set for all or most shells in a justfile.

One possible way of supporting this would be to define a snippet of shell code which should be prepended to all shell commands. This could be supported with a shell-prelude setting:

set shell-prelude := 'export FOO=bar;'

foo:
  echo $FOO

When running the recipe foo, the line echo $FOO would actually run export FOO=bar; echo $FOO.

One annoying thing is that the shell prelude would be executed before every line of a recipe, so if it were slow, it would significantly slow down execution.

casey avatar May 24 '24 02:05 casey

I agree that would be nice. I'd need a shorthand to call the various recipes I have, and an alias in a shell prelude would do wonder.

As of today, it seems there is no real other way to perform that than to hack away at the default shell invocation. Which brings to the following point: the documentation does not seem to document what the precise invocation of the default shell is.

gl-yziquel avatar Jun 05 '24 09:06 gl-yziquel

Good point, I documented this in #2129.

casey avatar Jun 05 '24 19:06 casey

How would this work, shell injection?

gyreas avatar Jul 07 '24 18:07 gyreas

It would actually be pretty easy, just prepend the prelude to the command before executing it. So here:

set shell-prelude := 'export FOO=bar;'

foo:
  echo $FOO

foo would actual execute export FOO=bar;echo $FOO.

casey avatar Jul 07 '24 20:07 casey

foo would actual execute export FOO=bar;echo $FOO.

I think newline should come after the prelude, in case someone tries to get exotic

gyreas avatar Jul 08 '24 09:07 gyreas

I actually think it should be up to the user to include whatever terminating whitespace they want. If just automatically prepends some kind of whitespace, either a space or a newline, the user won't have a way to easily override it, in case they don't want it.

casey avatar Jul 08 '24 16:07 casey

I think I understand how you intend it to be used. Cool then

gyreas avatar Jul 08 '24 20:07 gyreas

I would like to have this prelude, that may differ dependending on the shell used.

I have a bunch of function that i would like to define to more readability in the output

In CI, I have something like:

image

And I do not really see the difference between the command git lfs install -f and its output Updated git hooks. Using the -x bash option does not make the log more readable.

I'd like to have some function always defined in every subshell (colorful echo_and_run function in CI)

gsemet avatar Sep 17 '24 13:09 gsemet