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

Expand variables in the default syntax ${...:-$VAR} #22

Open
randoragon opened this issue Feb 21, 2024 · 0 comments
Open

Expand variables in the default syntax ${...:-$VAR} #22

randoragon opened this issue Feb 21, 2024 · 0 comments

Comments

@randoragon
Copy link

randoragon commented Feb 21, 2024

As the title says. Expanding variables in fallback values is standard POSIX behavior that's implemented in virtually all Linux shells (dash, bash, zsh, and many more I imagine). It is particularly useful for accessing XDG directories (my exact use case), e.g. "${XDG_DATA_HOME:-$HOME/.local/share}/". Would be great to have this.

Minimal reproducible example

fn main() {
    println!("{}", shellexpand::full("${NON_EXISTENT_VAR:-$HOME}/").unwrap());
}

expected output: /home/user/
actual output: $HOME/

Result with dash/bash/zsh

$ echo ${NON_EXISTENT_VAR:-$HOME}/
/home/user/

Technically this is recursive, i.e. echo ${ABC:-${DEF:-ghi}} in dash yields ghi if both $ABC and $DEF are undefined. But I understand that perfect POSIX-compliance might be too great an endeavor. IMO just a single-level expansion would be fine. Also an appropriate comment in the docs would be welcome.

randoragon added a commit to randoragon/music-tools that referenced this issue Feb 22, 2024
It turned out shellexpand has some limitations that bugged me
(netvl/shellexpand#22).

I opted to use the more low-level and popular "dirs" crate, which
allowed me to write a quite elegant abstraction in form of a new
path_from() function.
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

1 participant