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

Can I run just commands in current shell? #1957

Open
ccxuy opened this issue Mar 12, 2024 · 5 comments
Open

Can I run just commands in current shell? #1957

ccxuy opened this issue Mar 12, 2024 · 5 comments

Comments

@ccxuy
Copy link

ccxuy commented Mar 12, 2024

I want to do something like below, so I can retrieve some variable set from same shell:
var=a

justfile:
show:
echo $var

@ccxuy ccxuy changed the title BUG: wired bug in mod when cd to same folder close Mar 12, 2024
@ccxuy ccxuy closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2024
@ccxuy ccxuy reopened this Mar 12, 2024
@ccxuy ccxuy changed the title close Can I run just commands in current shell? Mar 12, 2024
@laniakea64
Copy link
Contributor

Why doesn't it work to export the shell variable as an environment variable?

Running commands in the same shell from which just is invoked is not possible in general, but for very simple cases, you could do something like

@show:
  echo eval {{quote('echo "$var"')}}

then run it as $(just show)

@ccxuy
Copy link
Author

ccxuy commented Mar 14, 2024

@laniakea64

@show:
  echo eval {{quote('echo "$var"')}}
echo eval 'echo $aaa'
eval echo $aaa

doing this could not read var from the same shell from which just is invoked.

In my scenario, I need to source some script, which import a bunch of variables and function in current shell, and since it only work on current shell and not exported, I could not easily pass it to subshell.

In another scenario, I need to do something like:

do_some_setting:
  a=123

do_a:
  echo $a

I could not pass variables between recipes, even just variables itself is not persistent among just commands. My workaround right now is to save thease variables in file and read it in every recipes, but the case is I may need to reuse these variable for many times :

do_some_setting:
  save_a_in_config

do_a:
  read_a_in_config

do_b:
  read_a_in_config

@laniakea64
Copy link
Contributor

since it only work on current shell and not exported,

What happens if you try to export the variables it sets?

For example, if the script does

var=test
var2='other test'

then, with the justfile in your original comment, you could do

$ export var var2
$ just show
echo "$var"
test

@ccxuy
Copy link
Author

ccxuy commented Mar 17, 2024

What happens if you try to export the variables it sets?

It should works fine. The problem is there are many variables and functions been set by a script, which means I need to export all of them, also it may contaminate other sciprt environment.

I tried dot-env file to preserve the envrionment but it seems not working very well. Some special characters could cause problem when auto loading them.

@casey
Copy link
Owner

casey commented May 15, 2024

You could do this with a shebang recipe, like so:

foo:
  #!/usr/bin/env bash
  source script.sh
  echo $VAR

Does that work?

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

3 participants