How do I declare a constant in shell script?

BashShell

Bash Problem Overview


I can't find this information from the internet, is it not possible to declare a final constant variable whose value will not change after first initialization?

Bash Solutions


Solution 1 - Bash

I believe you can do something like:

readonly DATA=/usr/home/data/file.dat

You can also do:

declare -r var=123

Solution 2 - Bash

Solution 3 - Bash

In bash you can mark a variable read only by declaring it via the builtin readonly like so:

readonly CONSTVAR=value

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOh Chin BoonView Question on Stackoverflow
Solution 1 - BashdiagonalbatmanView Answer on Stackoverflow
Solution 2 - BashAnders LindahlView Answer on Stackoverflow
Solution 3 - Bashal.View Answer on Stackoverflow