Issue
I am following a tutorial and they use the command
[[ -z "$PORT" ]] && export PORT=8080
and I don't fully understand what it's doing. My knowledge of bash commands is very basic and so I am not even sure what to google to figure this out.
The little knowledge I have suggests to me that this somehow checks if the env variable PORT
is set and if not set it to 8080
. However, I don't actually understand what is going on, except for the last part, export PORT=8080
.
Could anyone explain what the different operations are doing here?
Solution
Here is what is going on -
[[ -z "$PORT" ]]
is checking whether the length of the string in variable"$PORT"
is zero or Not.- the second part of
&&
is only evaluated if the first part is true.
So short answer is this -
if the length of the string "$PORT"
is zero then it will export a variable named PORT
which will have the value of 8080
otherwise it will not export the variable and will moved on to the next statement in bash script.
Answered By - y.kaf. Answer Checked By - Timothy Miller (WPSolving Admin)