Skip to content
Advertisement

bash if elif statement always prints first value

For some reason, the first value is always printed. I can’t figure out the reason behind it. Any ideas?

#!/bin/bash
config="update_release"
if [[ "$config"=="update" ]]; then
   schema_list="main_schemas"
elif [[ "$config"=="update_release" ]] ; then
   schema_list="release_schemas"
elif [[ "$config"=="update_maintenance" ]]; then
   schema_list="maintenance_schemas"
fi
echo $schema_list

I tried many things including single =, single [] but nothing seems to be working at all.

Advertisement

Answer

You need to add whitespace to the conditions:

#!/bin/bash
config="update_release"
if [ "$config" == "update" ]; then
   schema_list="main_schemas"
elif [ "$config" == "update_release" ] ; then
   schema_list="release_schemas"
elif [ "$config" == "update_maintenance" ]; then
   schema_list="maintenance_schemas"
fi
echo $schema_list
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement