Condiciones
Condiciones en Bash
Section titled “Condiciones en Bash”No hay booleanos nativos: los comandos devuelven 0 = verdadero, distinto de 0 = falso (true / false).
Operadores
Section titled “Operadores”| Concepto | Significado |
|---|---|
-eq -ne -lt -le -gt -ge | Comparar números. |
= / == | Cadenas iguales (= en [ ]). |
!= | Cadenas distintas. |
-z / -n | Vacía / no vacía. |
< / > | Orden lexicográfico (mejor en [[ ]]). |
[ "$a" -gt 10 ][[ -z "$1" ]][[ "$user" == "root" ]]if / test / [[ ]]
Section titled “if / test / [[ ]]”[ ] ≈ test. Espacios obligatorios tras [ y antes de ]. Preferir [[ ]] (más seguro, &&/|| dentro).
if [ 3 -gt 4 ]; then echo "Mayor que 4"elif false; then echo "Nunca"else echo "4 o menor"fi
if [ 10 -lt 14 ] && [ 10 -gt 12 ]; then echo "verdadero"fi
if [[ 10 -lt 14 && 10 -gt 12 ]]; then echo "verdadero"fiFormas equivalentes:
test condition[ condition ][[ condition || condition ]]
if test 10 -eq 10; then echo "10 es igual a 10"fiDocs: Bash Conditional Constructs.