Search code examples
bashshellfirewalld

Variable in firewall-cmd command


I'm trying to do a script in Bash to add some firewall rules.

The variable I'm passing isn't working, I guess I'm doing the substitution wrong.

firewall-cmd --add-rich-rule='rule family="ipv4" source address="$IP/32" port port=10000 protocol=tcp accept'

Returns

Error: INVALID_ADDR: $IP/32

What's wrong here, and how do I fix it?


Solution

  • You are single-quoting the entire string, so you are passing the literal string $IP. You can break out of the single quotes temporarily by adding a closing single quote before the variable and a new single quote after it.

    firewall-cmd --add-rich-rule='rule family="ipv4" source address="'$IP'/32" port port=10000 protocol=tcp accept'