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

Easier string interpolation of env variables #12750

Open
MilesCranmer opened this issue May 3, 2024 · 1 comment
Open

Easier string interpolation of env variables #12750

MilesCranmer opened this issue May 3, 2024 · 1 comment
Labels
enhancement New feature or request quoting/expansion Issues related to string quoting and expansion of variable or glob patterns syntax Changes to the grammar or syntax beyond parser bugfixes

Comments

@MilesCranmer
Copy link

MilesCranmer commented May 3, 2024

Related problem

I am loving nushell. I think what would make it even better would be to make env variable interpolation into strings easier.

In bash, I can use string interpolation of env variables as follows

cd $ENV_PREFIX/my/dir/to/$PACKAGE

In nushell, this would become the more verbose

cd $"($env.ENV_PREFIX)/my/dir/to/($env.PACKAGE)"

This is 15 more key strokes for the same command. Obviously it is safer and more readable, but it is also many more key strokes for a simple interpolation that I am using out of convenience.

Describe the solution you'd like

I would like to propose an "env string" macro syntax which automatically put the env variables into context before interpolating, so that I don't need to write out so many characters each time.

For example, the above command would become

cd e"($ENV_PREFIX)/my/dir/to/($PACKAGE)"

This is an 8-keystroke reduction for the same command, at no loss to safety or readability.

The e"..." would be a macro that automatically loads all env variables into context before interpolating as an $"...". Other variables would not be passed through. So for example, if you had

let x = "a"
echo $"($x)"  # Works
echo e"($x)"  # Fails!

this would fail to interpolate x unless you had defined $env.x. I believe this is the safer option in case you accidentally refer to a variable not in the env.

Describe alternatives you've considered

(Not applicable)

Additional context and details

Semi-related issue here: #2317. str expand is suggested for brace expansion. However, this does not work for interpolating env variables.

Happy to discuss other ideas here as well. Really anything that reduces the key strokes in string interpolation for env variables would be fantastic!

@MilesCranmer MilesCranmer added enhancement New feature or request needs-triage An issue that hasn't had any proper look labels May 3, 2024
@39555
Copy link

39555 commented May 3, 2024

I have the same complains but for $"" :) and not being able to use just ($env.XDG_CONFIG_HOME)"/nushell/"($env.PACKAGE) how it works in other shells. I made this aliases:

alias p = do { |...args| echo ...$args | path join  }
alias scd =  do --env { cd $in }
p $env.HOME .local bin | scd

Maybe?

def e [s] {
    mut s = $s
    loop {
        let var = ($s | parse --regex '.*\(\$(?<var>.+)\).*') | get -i var.0
        if ($var | is-empty) {
            break
        }
        let replacement = $env | get -i $var | default $var
        $s = ($s | str replace --all $"\($($var)\)" $replacement)
    }
    $s
}
e "($HOME)/.local/($APPDATA)/bin"

@sholderbach sholderbach added quoting/expansion Issues related to string quoting and expansion of variable or glob patterns syntax Changes to the grammar or syntax beyond parser bugfixes and removed needs-triage An issue that hasn't had any proper look labels May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request quoting/expansion Issues related to string quoting and expansion of variable or glob patterns syntax Changes to the grammar or syntax beyond parser bugfixes
Projects
None yet
Development

No branches or pull requests

3 participants