
HPR3722: Bash snippet - plurals in messages
11/08/22 • -1 min
Overview
Have you ever written a Bash script (or any shell script) where you generate a message like 'Found 42 files' and the day comes when it reports 'Found 1 files'?
Have you been irritated by this? I have, and I go to lengths to deal properly with (English) plurals in my Bash scripts.
Method 1
The simplest solution would be to use an 'if' statement:
if [[ $fcount -eq 1 ]]; then echo "Found 1 file" else echo "Found $fcount files" fiThis works, but to have to do it for every message would be a pain!
Method 2
The next approach to this problem might be to write a Bash function.
pluralise () { local singular="${1}" local plural="${2}" local count="${3}" if [[ $count -eq 1 ]]; then echo "$singular" else echo "$plural" fi }This can be called as follows:
$ i=1; echo "Found $i $(pluralise "file" "files" $i)" Found 1 file $ i=42; echo "Found $i $(pluralise "file" "files" $i)" Found 42 filesThe string being displayed with echo contains a command substitution ('$(command)') which returns 'file' or 'files' depending on the value given.
The first two arguments can be more complex than plain strings:
$ i=1; echo "There $(pluralise "is 1 light" "are $i lights" $i)" There is 1 light $ i=4; echo "There $(pluralise "is 1 light" "are $i lights" $i)" There are 4 lightsThe pluralise function is available for download.
Method 3
The GNU project has developed a set of utilities called the GNU gettext utilities consisting of tools and documentation for translation. This is a large subject which is not suitable for a short HPR episode such as this one.
Among the tools is 'ngettext' which performs the function we have been discussing - choosing among plural forms. It also implements translations if desired (and translation files are provided as part of the software being developed).
We will not discuss the translation topic here, but the choice of plurals is something that can be used in Bash scripts.
The 'ngettext' tool takes three mandatory parameters:
- MSGID - the singular form of the text
- MSGID-PLURAL - the plural form of the text
- COUNT - the value used to make the singular/plural choice
There are other optional parameters and options but they are not relevant here.
The tool can be used in exactly the same way as the 'pluralise' example above.
$ i=1; echo "There $(ngettext "is 1 light" "are $i lights" $i)" There is 1 light $ i=4; echo "There $(ngettext "is 1 light" "are $i lights" $i)" There are 4 lightsWhether you use this or a Bash function is your choice.
Conclusion
I have been using ngettext in my scripts since I discovered it. If you also need to provide messages in your projects in other languages then this might be a good idea.
I admit that my understanding of the GNU gettext project is superficial, so, on reflection it might be better to use a Bash function, since I don’t currently need all of the features GNU gettext provides.
Links
- The pluralise function. Either add it to the script you’re developing or use the 'source' command to load it.
- GNU gettext utilities, part of the GNU Translation Project.
Overview
Have you ever written a Bash script (or any shell script) where you generate a message like 'Found 42 files' and the day comes when it reports 'Found 1 files'?
Have you been irritated by this? I have, and I go to lengths to deal properly with (English) plurals in my Bash scripts.
Method 1
The simplest solution would be to use an 'if' statement:
if [[ $fcount -eq 1 ]]; then echo "Found 1 file" else echo "Found $fcount files" fiThis works, but to have to do it for every message would be a pain!
Method 2
The next approach to this problem might be to write a Bash function.
pluralise () { local singular="${1}" local plural="${2}" local count="${3}" if [[ $count -eq 1 ]]; then echo "$singular" else echo "$plural" fi }This can be called as follows:
$ i=1; echo "Found $i $(pluralise "file" "files" $i)" Found 1 file $ i=42; echo "Found $i $(pluralise "file" "files" $i)" Found 42 filesThe string being displayed with echo contains a command substitution ('$(command)') which returns 'file' or 'files' depending on the value given.
The first two arguments can be more complex than plain strings:
$ i=1; echo "There $(pluralise "is 1 light" "are $i lights" $i)" There is 1 light $ i=4; echo "There $(pluralise "is 1 light" "are $i lights" $i)" There are 4 lightsThe pluralise function is available for download.
Method 3
The GNU project has developed a set of utilities called the GNU gettext utilities consisting of tools and documentation for translation. This is a large subject which is not suitable for a short HPR episode such as this one.
Among the tools is 'ngettext' which performs the function we have been discussing - choosing among plural forms. It also implements translations if desired (and translation files are provided as part of the software being developed).
We will not discuss the translation topic here, but the choice of plurals is something that can be used in Bash scripts.
The 'ngettext' tool takes three mandatory parameters:
- MSGID - the singular form of the text
- MSGID-PLURAL - the plural form of the text
- COUNT - the value used to make the singular/plural choice
There are other optional parameters and options but they are not relevant here.
The tool can be used in exactly the same way as the 'pluralise' example above.
$ i=1; echo "There $(ngettext "is 1 light" "are $i lights" $i)" There is 1 light $ i=4; echo "There $(ngettext "is 1 light" "are $i lights" $i)" There are 4 lightsWhether you use this or a Bash function is your choice.
Conclusion
I have been using ngettext in my scripts since I discovered it. If you also need to provide messages in your projects in other languages then this might be a good idea.
I admit that my understanding of the GNU gettext project is superficial, so, on reflection it might be better to use a Bash function, since I don’t currently need all of the features GNU gettext provides.
Links
- The pluralise function. Either add it to the script you’re developing or use the 'source' command to load it.
- GNU gettext utilities, part of the GNU Translation Project.
Previous Episode

HPR3721: HPR Community News for October 2022
New hosts
Welcome to our new hosts:
Paul J, m0dese7en, CCHits.net Team.
Last Month's Shows
Id Day Date Title Host 3696 Mon 2022-10-03 HPR Community News for September 2022 HPR Volunteers 3697 Tue 2022-10-04 Mis-information, Dis-information, and Fake News. You are a product and target for all of it. Lurking Prion 3698 Wed 2022-10-05 Spectrogram Klaatu 3699 Thu 2022-10-06 Old and new videogames/board games with guest binrc Celeste 3700 Fri 2022-10-07 Introduction to Batch Files Ahuka 3701 Mon 2022-10-10 ReiserFS - the file system of the future Paul J 3702 Tue 2022-10-11 Easter Ogg Dave Morriss 3703 Wed 2022-10-12 McCurdy House Tour operat0r 3704 Thu 2022-10-13 Follow up to hpr3685 :: Budget and an Android app Archer72 3705 Fri 2022-10-14 The Year of the FreeBSD Desktop binrc 3706 Mon 2022-10-17 The Future of Technology Lurking Prion 3707 Tue 2022-10-18 Recovering a Massive 3.5 HP Electric Motor from a Treadmill MechatroniacNext Episode

HPR3723: HPR News
HPR News.
Threat Analysis; your attack surface.
Wireless key fobs compromised in European nations (France, Spain, and Latvia). On October 10, 2022, European authorities arrested 31 suspects across three nations. The suspects are believed to be related to a cybercrime ring that allegedly advertised an “automotive diagnostic solution” online and sent out fraudulent packages to their victims. The fraudulent packages contained malware and once installed onto the victims vehicle, the attackers were able to unlock the vehicle, start the ignition, then steal the vehicle without the physical key fob. European authorities confiscated over €1 million in criminal assets (malicious software, tools, and an online domain).
Microsoft Office 365 has a broken encryption algorithm. Microsoft Office 365 uses an encryption algorithm called “Office 365 Message Encryption” to send and receive encrypted email messages. The messages are encrypted in an Electronic Codebook (ECB). The U.S. National Institute of Standards and Technology (NIST) reported, "ECB mode encrypts plaintext blocks independently, without randomization; therefore, the inspection of any two ciphertext blocks reveals whether or not the corresponding plaintext blocks are equal". Emails can be harvested today then decrypted later for future attacks.
User Space.
Netflix crackdown on freeloaders. Netflix is testing in Argentina, the Dominican Republic, El Salvador, Guatemala, and Honduras Chile, Costa Rica and Peru different efforts to crackdown on freeloaders. The term “freeloaders” covers the multiple users sharing a single Netflix account from different locations. Netflix plans to charge an additional $3.00 - $4.00 per subaccount.
Samsung implements private blockchain to link user devices. While claiming the private blockchain, “has nothing to do with cryptomining”, the Knox Matrix security system links all your devices together in a private blockchain instead using a server based group verification system. The system, Knox Matrix, is suppose to allow devices to “manage themselves” by auto updating, caching updates for other devices then distributing the updates to other devices on the private blockchain.
Toys for Techs.
Juno Tablet is a Beta product; overall it works with a few bugs. This is a non-refundable product, you will only get store credit.
- Price: $429.00 USD.
- Screen Size: 10.1”
- Screen Type: Full HD IPS screen 1920×1200 Capacitive touch, Capacitive (10-Point) MIPI-DSI.
- Refresh Rate: 60 Hz.
- CPU: Intel Jasper Lake Intel Celeron N5100 (4 Cores / 4 Threads) – 1.10GHz (Turbo 2.80 GHz)
- Graphics: Intel UHD Graphics, Frequency: Base 350 MHz - Max 800 MHz.
- Ram (SOLDERED): 8GB 2133 MHz LPDDR4.
- Storage: 256GB, 512GB, 1TB SSD.
- Chassis: Plastic.
- Wireless Card: Intel Wireless AC 9460/9560 Jefferson Peak 2.4 and 5GHz + Bluetooth 4.2.
- Ports:
- 1x USB3.0
- 1x Type-C 3.1 (Supports charging + video out)
- 1x Mini HDMI
- 1x Micro SD
- 3.5MM Headphone Jack
- Built-in Microphone
- Linux Kernel 5.18+
- OS:
- Manjaro Phosh
- Manjaro Plasma Mobile
- Mobian Phosh
- Windows 11 (Not included – can provide ISO)
- JingPad A1, It’s the World’s FIRST Consumer-level ARM-based Linux Tablet.
- JingPad A1 maybe discontinued: https://www.youtube.com/watch?v=...
If you like this episode you’ll love
Episode Comments
Generate a badge
Get a badge for your website that links back to this episode
<a href="https://goodpods.com/podcasts/hacker-public-radio-201136/hpr3722-bash-snippet-plurals-in-messages-24742829"> <img src="https://storage.googleapis.com/goodpods-images-bucket/badges/generic-badge-1.svg" alt="listen to hpr3722: bash snippet - plurals in messages on goodpods" style="width: 225px" /> </a>
Copy