base-files: functions.sh: Add prepend() homologue to append()
authorPhilip Prindeville <philipp@redfish-solutions.com>
Sun, 24 Sep 2023 17:52:19 +0000 (11:52 -0600)
committerPaul Spooren <mail@aparcar.org>
Mon, 23 Oct 2023 17:18:04 +0000 (19:18 +0200)
Sometimes it's useful to be able to prepend to a variable as
well, such as when dealing with domain names, e.g.

    prepend fdqn "$subdomain" "."

will result in:

   fqdn="$subdomain.$fqdn"

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
package/base-files/files/lib/functions.sh

index bbdecbbc47586dc8a0376b0215ce415dff9c03b7..d9628dbb7ad2b87a79bd42a82544f73000397f82 100644 (file)
@@ -40,6 +40,14 @@ append() {
        eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
 }
 
+prepend() {
+       local var="$1"
+       local value="$2"
+       local sep="${3:- }"
+
+       eval "export ${NO_EXPORT:+-n} -- \"$var=\${$value:+\${$value}\$sep}\$var\""
+}
+
 list_contains() {
        local var="$1"
        local str="$2"