Bash script error: ./test: line 5: UID: readonly variable
Solution 1:
It's a good idea to use lowercase variables in your shell scripts, as uppercase variables are usually reserved for shell internals. (Environment variables are an exception to this, but should really be treated the same way, as things with special meanings that you shouldn't arbitrarily change without understanding what you're doing.) In this case, bash
sets $UID
to the Unix uid it's running under, and won't allow it to be changed or the variable to be used for anything else. Shells other than bash
(sh
may be dash
or some other shell on some platforms) may not use $UID
that way, and if bash
was built to work in Bourne shell compatibility mode when invoked as sh
(which is the default, but many Linux distributions disable that) it won't treat $UID
as special either.
Solution 2:
UID
is a system reserved variable with information about the user id your script is running as. You should be careful not to use standard variable names that the system might be using. You can use env
to get a list of what is currently set. Don't change any of those. You might also google variable names to see if they turn up in other users before you go ahead with them.
In this case, using lower case will probably fix your problem since only the upper case variants are reserved, but you could also make your variables unique by calling them something like $input_uid so it's clear that value came from input to the script. This will make your code easier to maintain in the long run.