Skip to content

Commit

Permalink
bash: configure default value of BASH_LOADABLES_PATH
Browse files Browse the repository at this point in the history
See: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-enable
See: http://git.savannah.gnu.org/cgit/bash.git/tree/config-top.h#n80

This enables shorter syntax for loading stock Bash builtins. For example,
instead of doing this:

    enable -f $(brew --prefix)/lib/bash/csv csv

the user can now load the `csv` builtin directly:

    enable csv

To complete the example, we can now do this:

    $ echo $BASH_LOADABLES_PATH
    /opt/homebrew/Cellar/bash/5.2.32/lib/bash:/usr/local/lib/bash:/usr/lib/bash:/opt/local/lib/bash:/usr/pkg/lib/bash:/opt/pkg/lib/bash:.
    $ type csv
    -bash: type: csv: not found
    $ enable csv
    $ type csv
    csv is a shell builtin
    $ help csv
    csv: csv [-a ARRAY] string
       Read comma-separated fields from a string.

       Parse STRING, a line of comma-separated values, into individual fields,
       and store them into the indexed array ARRAYNAME starting at index 0.
       If ARRAYNAME is not supplied, "CSV" is the default array name.
    $ csv 'a,"b",c'
    $ declare -p CSV
    declare -a CSV=([0]="a" [1]="b" [2]="c")
    $ enable -n csv
    $ type csv
    -bash: type: csv: not found
  • Loading branch information
igstan committed Sep 20, 2024
1 parent 2b99a9f commit a32dcef
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Formula/b/bash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,24 @@ def install
# Homebrew's bash instead of /bin/bash.
ENV.append_to_cflags "-DSSH_SOURCE_BASHRC"

bash_loadables_path=[
"#{lib}/bash",
# Stock Bash paths; keep them for backwards compatibility.
"/usr/local/lib/bash",
"/usr/lib/bash",
"/opt/local/lib/bash",
"/usr/pkg/lib/bash",
"/opt/pkg/lib/bash",
".",
].join(":")
ENV.append_to_cflags "-DDEFAULT_LOADABLE_BUILTINS_PATH='\"#{bash_loadables_path}\"'"

system "./configure", "--prefix=#{prefix}"
system "make", "install"
end

test do
assert_equal "hello", shell_output("#{bin}/bash -c \"echo -n hello\"")
assert_equal "hello", shell_output("#{bin}/bash -c 'echo -n hello'")
assert_equal "csv is a shell builtin\n", shell_output("#{bin}/bash -c 'enable csv; type csv'")
end
end

0 comments on commit a32dcef

Please sign in to comment.