You are not logged in.

#1 2022-07-28 17:56:31

anick
Member
Registered: 2022-07-07
Posts: 10

What's the correct location for "lib" files sourced by bash scripts?

I have some "library" files  I source in many different bash scripts.

I want to make a package (for my personal use), and my question is, if the correct location for these files is

/usr/lib/mybashlib/

?


(PS: How does one format 'inline monospaced text' in BBCode? [c]{text}[/c] did not work for me.)

Offline

#2 2022-07-28 18:01:39

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,263

Re: What's the correct location for "lib" files sourced by bash scripts?

Since it's "your" lib, yeah why not, since they are technically not libs but other bash/data files potentially a subdir in /usr/share might be more applicable but as long as you don't have a conflict with other packages it probably doesn't strictly matter

PS: You can't do inline tags.

Offline

#3 2022-07-28 18:03:43

anick
Member
Registered: 2022-07-07
Posts: 10

Re: What's the correct location for "lib" files sourced by bash scripts?

Would you, personally, put them under /usr/share/mybashlib/ ?

Thank you, btw.

And another (stupid) question: How are naming conflicts of packages resolved? Does the first one, that claims a name win? And how/where can someone check if a name is already in use? (Is https://archlinux.org/packages/ the definite authority, even for stuff not yet available as an Arch package?)

Last edited by anick (2022-07-28 18:19:28)

Offline

#4 2022-07-28 18:11:00

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,665

Re: What's the correct location for "lib" files sourced by bash scripts?

If it is for your personal use, I would put it in ~/.local/bin and ensure that is in your PATH.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Online

#5 2022-07-28 18:18:21

anick
Member
Registered: 2022-07-07
Posts: 10

Re: What's the correct location for "lib" files sourced by bash scripts?

I now think, that I shouldn't have said, that it's only for "personal use".

I really want to know the "correct location" these files belong in (like if it would be a public package, intended for many to install).

Last edited by anick (2022-07-28 18:22:47)

Offline

#6 2022-07-28 19:02:48

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,501

Re: What's the correct location for "lib" files sourced by bash scripts?

mkinitcpio has its sources in /usr/lib/initcpio/ makepkg has its stuff in /usr/share/makepkg/ bash has /usr/share/bash-completion/ zsh has /usr/share/zsh/functions …
There's no "correct" but for the most part it seems /usr/share is preferred over /usr/lib but it also really doesn't matter - what matters is that you have a collision-free path, so /usr/share/myscript/foo.sh is bad - /usr/lib/anicksbashtricks/bar.sh is better.

Offline

#7 2022-07-28 19:17:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: What's the correct location for "lib" files sourced by bash scripts?

anick wrote:

How are naming conflicts of packages resolved?

They aren't really.  But that's why you use something like /usr/share/${pkgname}.  As long as no other package has the same name as yours, then no other package should use that path.  And package name conflicts are avoided pretty much by first come first serve as a duplicate name really can't be added to the repo or aur (and an aur package that duplicated a repo name should be quickly removed / deleted).  I suspect there are cases where package maintainers were asked to rename their package if they had a small project that took the same name as a well-known and established project which was later going to be added to the repos.  And any good programmer should make a genuine effort to not name their project the same thing as a different existing project.

But aside from paths including pkgname, there are file conflicts even between some repo packages.  This is mostly avoided by giving files like actual shared libraries appropriate names.  On occasion some newbie programmer will think something as minimal as "libm.so" was suitable for their niche project - but this will not last long: you've got to be a glibc-level badass to have the stones to claim a name like that.

Last edited by Trilby (2022-07-28 19:18:45)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2022-07-28 19:29:36

Xyne
Forum Fellow
Registered: 2008-08-03
Posts: 6,965
Website

Re: What's the correct location for "lib" files sourced by bash scripts?

A subdirectory under /usr/share or ~/.local/share (or whatever $XDG_DATA_HOME points to) is usually a good place for miscellaneous files related to a specific program or use.

You can also use relative paths if you want to keep everything together in a single directory without hard-coding a full path to the files that you want to source.

Example directory structure

example/
├── lib
│   ├── bar.sh
│   └── foo.sh
└── scripts
    └── test.sh

test.sh

#!/usr/bin/env bash
set -euo pipefail

this_script=$(readlink -f "${BASH_SOURCE[0]}")
root_dir=${this_script%/*/*}
source "$root_dir"/lib/{foo,bar}.sh

Then you can put the example directory wherever you want and just add the scripts subdirectory to PATH.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#9 2022-07-28 19:35:08

anick
Member
Registered: 2022-07-07
Posts: 10

Re: What's the correct location for "lib" files sourced by bash scripts?

About this "libm.so" thing: Is it allowed that different packages have files under the same path? I mean something like that: packageA has /usr/lib/x/a and packageB has /usr/lib/x/b ?

Offline

#10 2022-07-28 19:44:25

Xyne
Forum Fellow
Registered: 2008-08-03
Posts: 6,965
Website

Re: What's the correct location for "lib" files sourced by bash scripts?

anick wrote:

About this "libm.so" thing: Is it allowed that different packages have files under the same path? I mean something like that: packageA has /usr/lib/x/a and packageB has /usr/lib/x/b ?

If the packages are related in some non-trivial way, i.e. they're both part of some project "x", then yes. If they have nothing to do with each other then they shouldn't be in the same subdirectory. Name collisions occur and there may be some cases in which 2 unrelated packages could co-exist that way, but it's confusing and bad practice.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#11 2022-07-28 19:48:05

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,501

Re: What's the correct location for "lib" files sourced by bash scripts?

Not sure how non-trivial that is but otherwise there would be the execption of modules/plugins, eg.

pacman -Qo /usr/lib/security

which also has real™ libraries inside and the projects dropping them there are not related to each other - other than providing plugins for "stuff".

Maybe be more specific about your intentions?

Offline

#12 2022-07-28 20:05:55

anick
Member
Registered: 2022-07-07
Posts: 10

Re: What's the correct location for "lib" files sourced by bash scripts?

> Maybe be more specific about your intentions?

Hmm. My intentions? I don't know, TBH. I put some general stuff I always repeated when writing bash scripts into some files and I'm genuinely wondering where to put them, because neither the FHS, nor the people over at unix.stackexchange.com (https://unix.stackexchange.com/question … es-sourced) seem to have an answer for that. That's why and that's all.

But now I know, thanks to you. /usr/share/<aname>/ seems to be the answer you're suggesting.

I should have asked this question here first, but I wanted a more distro-agnostic answer (- which did not work out, because some stackexchange mod tagged my question with 'arch-linux' (and altered its wording, too). But the question I asked was "Where to put files intended to be 'sourced' by bash scripts?").

Last edited by anick (2022-07-28 20:48:38)

Offline

Board footer

Powered by FluxBB