I have a script that on startup of an application is looking for the host. I am declaring a variable at the beginning of the script VAR=$(uname -n)
and calling that variable as needed throughout the script. Is there any advantage/disadvantage to just using $(uname -n)
throughout the script instead of just calling the variable each time?
If you use a variable it is slightly more efficient, as it will spawn the program only once. You are also sure that the value does not change (which is not typically the case). You also need to handle execution errors only once, so I would say: go for it.
(And you should use the full path to uname or be sure to sanitize the PATH before using relative commands).
BTW: if you call the variable UNAME instead of VAR, then it is also less confusing :) BTW2: the uts_name you get from this method might not always be the right hostname. Hard to say without knowing for what you use it.