Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a constant in shell script?

Tags:

bash

shell

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?

like image 779
Oh Chin Boon Avatar asked Aug 11 '11 08:08

Oh Chin Boon


People also ask

How do you create a constant in a shell script?

You can create the constants variables using the readonly command or declare command.

How do you declare constant?

You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.

How do you declare and initialize a constant?

A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type. Constant variables can be declared for any data types, such as int , double , char , or string .

How do you declare a variable in a bash script?

There are no data types. A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.


2 Answers

I believe you can do something like:

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

You can also do:

declare -r var=123 
like image 111
diagonalbatman Avatar answered Oct 16 '22 16:10

diagonalbatman


readonly FOO=bar

​​​​​

like image 32
Anders Lindahl Avatar answered Oct 16 '22 17:10

Anders Lindahl