这是indexloc提供的服务,不要输入任何密码
Skip to content

Support conditional interactive shell code #31

@Neved4

Description

@Neved4

To implement

  • Emit if status is-interactive on case $- in *i*)

Description

This may be a specific use case, but is valuable in a lot of scenarios.

Consider the following script:

f() {
    echo 'f function you also want to source'
}

case $- in
*i*) return
esac

main() {
    f
}

main

In the case statement, we check if the shell is interactive. If it is, the script exits early. This allows the f() function to be imported into our environment without needing to manually add it to .profile. As a result, the script can be executed or sourced without extra redundancy.

Most of my scripts, if not all, take this form, as it allows them to gracefully handle being both sourced and executed without duplicating helper functions in the shell .profile.

Currently, babelfish will try to transliterate it as-is:

switch "$-"
case '*i*'
  return
end

That previous code will fail, but iwe can achieve a similar result inn fish with:

if status is-interactive
    return
end

We could emit that code whenever a simple case $- in *i*) is detected.

Any thoughts or feedback are welcome! 💙

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions