diff options
author | mapc <musikmichael@web.de> | 2012-05-05 10:39:31 +0200 |
---|---|---|
committer | mapc <musikmichael@web.de> | 2012-05-29 01:51:50 +0200 |
commit | 8f71efc09b42d69cc053649fade92485d282a561 (patch) | |
tree | 7b41a810ac27574b258208abc92eb801236ebb13 /lib/functions.zsh | |
parent | dbef8b1a92f789f4109435a3b7278f899e328767 (diff) | |
download | zsh-8f71efc09b42d69cc053649fade92485d282a561.tar.gz zsh-8f71efc09b42d69cc053649fade92485d282a561.tar.bz2 zsh-8f71efc09b42d69cc053649fade92485d282a561.zip |
Add helper to easily define default values for variables and env variables.
Diffstat (limited to 'lib/functions.zsh')
-rw-r--r-- | lib/functions.zsh | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/functions.zsh b/lib/functions.zsh index b4d89a64a..3770d4e82 100644 --- a/lib/functions.zsh +++ b/lib/functions.zsh @@ -44,4 +44,32 @@ function alias_value() { # function try_alias_value() { alias_value "$1" || echo "$1" +} + +# +# Set variable "$1" to default value "$2" if "$1" is not yet defined. +# +# Arguments: +# 1. name - The variable to set +# 2. val - The default value +# Return value: +# 0 if the variable exists, 3 if it was set +# +function default() { + test `typeset +m "$1"` && return 0 + typeset -g "$1"="$2" && return 3 +} + +# +# Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined. +# +# Arguments: +# 1. name - The env variable to set +# 2. val - The default value +# Return value: +# 0 if the env variable exists, 3 if it was set +# +function env_default() { + env | grep -q "^$1=" && return 0 + export "$1=$2" && return 3 }
\ No newline at end of file |