Introduction
Read-only variables help keep important values safe from accidental changes in your script.
readonly VARIABLE_NAME=VALUE
# or
readonly VARIABLE_NAMEreadonly PI=3.14 # Now PI cannot be changed
NAME="Alice" readonly NAME # NAME is now read-only
readonly MAX_USERS=100 MAX_USERS=200 # This will cause an error
#!/bin/bash readonly GREETING="Hello" echo $GREETING # Trying to change GREETING GREETING="Hi" echo $GREETING
readonly command do in a bash script?readonlyreadonly command is used to mark a variable so it cannot be changed later in the script.readonly = makes variable unchangeable [OK]MYVAR read-only in bash?readonlyreadonly MYVAR to make it read-only.readonly MYVAR. Options C and D are not valid bash syntax.readonly VAR after assignment [OK]MYVAR=hello readonly MYVAR MYVAR=world echo $MYVAR
MYVAR is assigned "hello" and then made readonly. The script then tries to change it to "world".MYVAR remains "hello". The echo command outputs the original value.readonly MYVAR =10 MYVAR=20 echo $MYVAR
readonly cannot be combined with assignment like readonly MYVAR =10. The variable must be assigned first, then made readonly.readonly, not assignments.CONFIG_PATH so it cannot be changed accidentally in your script, but you also want to print its value later. Which sequence of commands is correct?CONFIG_PATH a value, then use readonly to lock it.Pass the test and this pattern moves unknown