Search code examples
bashwhiptail

Using whiptail with variable containing dash ('-')


I want to display a menu in linux bash using whiptail
When a menu item contains one or more dashes, whiptail fails to show the menu.
Example with only one item:

MQTT_PublisherArgs='-h {host} -t {topic} -m "{{message}}"'
whiptail --title "MQTT Configuration" --menu "" 0 70 0 "Publisher Arguments" "$MQTT_PublisherArgs"

When I replace the dashes with another character e.g. underscore, the menu is shown correctly.
I tried to escape the dashes with \- but the backslash is shown in the menu.

How can I display the menu item -h {host} -t {topic} -m "{{message}}"

When the user selects this item he will prompted to enter his proper arguments


Solution

  • Replace

    "$MQTT_PublisherArgs"
    

    with

    -- "$MQTT_PublisherArgs"
    

    to tell whiptail that the following are not options.

    From man whiptail:

    whiptail interprets arguments starting with a dash "-" as being arguments. To avoid this, and start some text in, for example, a menubox item, with a dash, whiptail honours the getopt convention of accepting the special argument "--" which means that all following arguments with dashes are to be treated verbatim and not parsed as options.