Re: Suggestion for .bashrc

2022-01-04 Thread Matthew Persico
On Fri, Dec 31, 2021 at 10:33 AM Greg Wooledge  wrote:

> On Thu, Dec 30, 2021 at 08:46:16PM -0800, Kevin O'Gorman wrote:
> > So I propose extending  the stanza near the end of .bashrc:
> > if [ -f ~/.bash_aliases ]; then
> > . ~/.bash_aliases
> > fi
>
> That code was written by your operating system vendor, or by your
> system administrator, or by you.  It's not something that comes from
> upstream bash.
>
> > by following it with
> > for __bash_alias in ~/.bash_aliases.d/* ; do
> > if [ -f $__bash_alias ]; then
> > source $__bash_alias
> > fi
> > done
>
> You can edit the file yourself and make it work however you like.  You
> have the right idea, but I'd write it like this:
>
> if [[ -d ~/.bash_aliases.d ]]; then
> for f in ~/.bash_aliases.d/*; do
>

Doesn't that shell expansion in the 'for' loop need to be protected somehow
so that if there are no files in the directory, the loop does not try to
process literally the file '*' in that directory?

[[ -f $f ]] && source "$f"
> done
> fi
>
> Variable expansions need to be quoted inside [ but not inside [[.
>
>

-- 
Matthew O. Persico


Re: Suggestion for .bashrc

2022-01-04 Thread Greg Wooledge
On Tue, Jan 04, 2022 at 03:34:05PM -0500, Matthew Persico wrote:
> On Fri, Dec 31, 2021 at 10:33 AM Greg Wooledge  wrote:
> > You can edit the file yourself and make it work however you like.  You
> > have the right idea, but I'd write it like this:
> >
> > if [[ -d ~/.bash_aliases.d ]]; then
> > for f in ~/.bash_aliases.d/*; do
> >
> 
> Doesn't that shell expansion in the 'for' loop need to be protected somehow
> so that if there are no files in the directory, the loop does not try to
> process literally the file '*' in that directory?
> 
> [[ -f $f ]] && source "$f"
> > done
> > fi

That's precisely what the [[ -f $f ]] check is for.  You could use -e
instead of -f if you prefer, just in case you ever want it to be able
to source named pipes or something.