I'm trying to understand a function created by > airmon-ng but when I get to this conditional line:
if [ ! "${InterfacePhysical// /}" ]; then
InterfacePhysical="$(ls -l "$interface_physical_path" | sed 's/^.*\/\([a-zA-Z0-9_-]*\)$/\1/')"
fi
Taking into account that InterfacePhysical="phy0"
What I don't understand is:
Why is the variable enclosed in braces? And what is the functionality of those "// /" that are between the braces?
If someone can make me understand what validates this condition, it would be very helpful.
TL;DR
It is evaluating if the variable, removing the spaces, is empty or not.
extended explanation
There is a Bash mechanism called parameter expansion .
We can invoke this mechanism using curly braces
${variable}
. Now that we have this, we can call other "sub-mechanisms" (personal term) like substitution:Which implies that the variable variable is going to be searched for a pattern and replaced with the (obvious) replacement. But normally only the first pattern found will be replaced :
And only the first letter "a" was changed to a zero.
Instead, if we start the pattern with the character
/
, all matches are changed :Now we know that in your case:
In the variable
InterfacePhysical
, the mechanism searches for all matches of the pattern of spaces, then removes them. Namely:And if we translate it into Spanish:
Using just a forward slash
/var/pattern/string
would only delete the first space it finds.After we have a variable without spaces, we proceed to do a simple evaluation:
That is, we are evaluating if the variable, removing the spaces, is empty.
And negating the condition:
That is, it will only enter the block
if
if the variable is empty.