I found a pretty good script builder site called Argbash. You feed it a "template" which will build a skeleton script for you. For my example, create a file in your user home directory ~/sub.conf with your default values:
topic="#"
user="mqtt"
password="mqttpass"
server="127.0.0.1"
Next, go to argbash's template creation page and feed it this template:
#!/bin/bash
# version="0.1"
#
# This is an optional arguments-only example of Argbash potential
#
# ARG_OPTIONAL_SINGLE([user], [u], [optional argument help msg])
# ARG_OPTIONAL_SINGLE([Password], [P], [optional argument help msg])
# ARG_OPTIONAL_SINGLE([server], [s], [optional argument help msg])
# ARG_OPTIONAL_SINGLE([topic], [t], [optional argument help msg])
# ARG_HELP([The general script's help msg])
# ARGBASH_GO
# [ <-- needed because of Argbash
echo "Value of --user: $_arg_user"
echo "Value of --Password: $_arg_password"
echo "Value of --server: $_arg_server"
echo "Value of --topic: $_arg_topic"
# ] <-- needed because of Argbash
And then click the "GENERATE SCRIPT NOW" button. It will generate a script for you which you can then download. Inside of that script search for:
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_user=
_arg_password=
_arg_server=
_arg_topic=
Simply change that to:
# THE DEFAULTS INITIALIZATION - OPTIONALS
. ~/.sub.conf
_arg_user=$user
_arg_password=$password
_arg_server=$server
_arg_topic=$topic
. ~/.sub.conf will load the values from your config file into the variables specified in that file. The following lines will populate each command line argument you specified in your template. Any values that are passed into your script will override those default values. You can remove the echo "Value of --user: $_arg_user statements if you wish. At the very end of the script, simply consume the arguments, such as:
mosquitto_sub -u $_arg_user -P $_arg_password -h $_arg_server -t $_arg_topic