just icon indicating copy to clipboard operation
just copied to clipboard

Can I run just commands in current shell?

Open ccxuy opened this issue 1 year ago • 8 comments

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

justfile: show: echo $var

ccxuy avatar Mar 12 '24 09:03 ccxuy

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)

laniakea64 avatar Mar 12 '24 15:03 laniakea64

@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

ccxuy avatar Mar 14 '24 08:03 ccxuy

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

laniakea64 avatar Mar 14 '24 19:03 laniakea64

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.

ccxuy avatar Mar 17 '24 08:03 ccxuy

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

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

Does that work?

casey avatar May 15 '24 02:05 casey

Not really, if the environment could not pass to other command, I need to repleatly source some script and sometimes need to workaround for some side effect from other commands. (most of them likely some environment issue, or maybe subprocess)

command_a:
  #!/bin/bash
  source {{justfile_directory()}}/env.sh > /dev/null
  command a

command_b:
  #!/bin/bash
  source {{justfile_directory()}}/env.sh > /dev/null
  # need to do some trick to repeat what happend in command a
  command b

ccxuy avatar Jun 15 '24 11:06 ccxuy

this work fine for adding export PATH=etc...

run:
    #!/bin/bash
    source env.sh
    run command

CT3 avatar Aug 29 '24 10:08 CT3

this work fine for adding export PATH=etc...

run:
    #!/bin/bash
    source env.sh
    run command

I realized the problem is env.sh contain some code that could not be execute in just, ${BASH_SOURCE[0]} return empty result

cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd

ccxuy avatar Aug 30 '24 07:08 ccxuy

For powershell user.

alias rim := reimport-module
reimport-module:
	'gci *psd1 | %{ipmo $_}' # single quote string. Supported in powershell (and nushell).

Then in current shell session.

just rim | iex

Multiline string is a bit elaborated, we need [script] for that.

About nushell, I havent acknowledged of any method eval commands from string, may need to explore more.

groutoutlook avatar Aug 02 '25 13:08 groutoutlook