Bash Tip: Separate a Variable Name From a String

Assume you have this variable in bash:


singular=egg

Now you want to define a new variable which takes $singular and appends an “s” to it.

This is how to do it


singular=egg

plural=${singular}s

The trick is to use {} in order to separate the variable name from the rest of the string.

Enjoy

Leave a Reply