2

ahoy all.

while finding a few answers related to determining the locale's decimal separator (AKA radix point, thousands separator) in a shell script, the only answers that i have found related to obtaining the currency symbol of the current locale are specific to a programming language (e.g. PHP, Java, etc). however surprisingly i cannot yet find a solution which can be used in a POSIX shell script. i have come across references to locale variables like LC_MONETARY and LC_CURRENCY but have not yet stumbled upon a way to access them from the shell as they are not in the default environment of a normal bash/dash login.

as always thanks for reading and for any answers and comments.

peace.

2 Answers 2

3

after some more spelunking in the locale command i have discovered the following method for getting the currency symbol of the current locale:

locale currency_symbol 

this command and keyword returns the appropriate currency symbol of the current locale

$ LANG=it_IT.UTF-8 locale currency_symbol € $ LANG=en_US.UTF-8 locale currency_symbol $ 

of course any other solutions and options, including ones that avoid the execution of an external command (i.e. locale) and a sub-process, are always appreciated :)

Sign up to request clarification or add additional context in comments.

3 Comments

locale currency_symbol doesn't output anything on my macOS
It does for me, @Fravadona. What locale does locale with no args say you're in? Perhaps you're in one with no defined currency_symbol (like C or POSIX).
@pilcrow You're right, locale gives me this: LANG="en_US.utf8" LC_MONETARY="C" LC_ALL= (the others are all defined as C)
0

This should work in Bash 4.3 and higher:

declare -A lc_monetary while IFS='=' read -d $'\n' -r k v; do lc_monetary[$k]="$v" done < <(locale -k LC_MONETARY) 

You can then access all values related to LC_MONETARY via the associative array lc_monetary. For example, to access the currency symbol:

currsym=lc_numeric["currency_symbol"] 

To get the full list of keys and values, use

declare -p lc_monetary 

For the list of keys, do

echo "${!lc_monetary[@]}" 

See the locale (5) man page for the different keys and their meaning.

1 Comment

while i am looking for a POSIX solution as mentioned in my question, thanks for this bash option

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.