bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Feature request


From: Brad Hayes
Subject: Feature request
Date: Wed, 6 Jul 2022 13:25:38 +1000

Hey, guys I just joined to ask this please forgive me if I am in the wrong
place.


First of all big fan of bash, I use it a LOT 🚀.


It would be fantastic if there was a nicer way to source files from the
same folder instead of copy-pasting complicated logic in every script.


Perhaps something similar to PHP's __DIR__ and __FILE__ constants?

```

source "$__DIR/file.ext" #$__DIR == the absolute path to the current
scripts location.

```

I am aware there are existing bash mechanisms to achieve this but are
cumbersome to use.


USE CASE:


I've been building myself a little ecosystem with hundreds of bash scripts.
Its really handy to have the script's source and call each other without
doing path resolving gymnastics in every single script.

To make it nice to rapidly build scripts at will I have been putting common
code into smaller scripts.

And to solve the problem of easily sourcing them I just shoved them in my
path lol.


This lets me use a nice clean syntax like so:


```

#!/usr/bin/env bash

source error-handler

source colours

#etc..

```


While this makes including common files an absolute breeze it's not ideal
to have all of them in my path because:


   - my sourceables also need logic to prevent them from being executed by
   accident (they tell you to source them instead).
   - I cant just share my script collection with colleagues because it will
   all break unless they add ALL my scripts to their system path and they
   might not want to do that.
   - all of the scripts call on each other and that also only works if the
   executables are in the system path.

Defining a ton of functions and shoving them into peoples running
environment is not an answer for me either lol.


A shorthand to call them from the relative path would be immensely helpful
for using bash scripting as a way to build suites of developer tools.


ADDITIONAL FEATURE:

This might be a little far fetch but, now that I think about it.


A pseudo namespacing feature would be way handier, hear me out...


Let's say you have a namespace keyword followed by the __DIR__ shorthand.
And once that is done it will treat that folder as if it is at the front of
the PATH var for the duration of the script.


Then we can call upon all the local resources in the distributed scripts
collection without jumping through hoops:


```

namespace $__DIR #$PATH = "$__DIR:$PATH" for the duration of the script.

source error-handler

DATA=some-common-command-in-my-library

# etc..

```


This would also mean that it can be easy to add a wrapper command to a
collection of scripts named after the vendor so that,

instead of directly calling from a collection of 300 scripts or adding them
all to your path,

you can simply add the one parent script to your path that namespaces and
then calls all the others while providing bash completions etc.



FOOTNOTE:


I am aware I could already do this with existing bash mechanisms there is
always a way.

But, it's usually a hacky way I can't remember and always have to copy from
an old script or look it up again and sometimes these hacks are not going
to work on other systems. (talking in general not just about path resolving
here now).


I just feel we should be able to have some basic language features to make
bash scripting better in general and being able to easily include code from
your own folder would be a significant step up.


Thanks for reading all this hope it makes sense.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]