If you’ve spent any significant amount of time writing scripts for the Bash or tcsh environments, then you’ve more than likely used the echo command to pass data to the user. This command can echo environment variables as well as messages, but there’s an alternative if you’re merely trying to send a message over to the user if you were writing, say, an install script. This alternative allows your message to appear in a window, which is significantly more user friendly. It also should appear more modern in many circumstances.
Technically the xmessage command could additionally be used to transmit environment variables to the user. The syntax is identical to echo, so if you’re already used to using this command then you should have no additional difficulties using xmessage in its place.
Method 1: Usage of the xmessage Command in Place of Echo
While you could use these commands from a run dialog box opened by holding down the Windows key and pushing R or even the CLI prompt, they’re most useful from inside of a script. Say you wanted to pause an installation script and prompt the user for input. Add this line to your script:
xmessage Tap the okay button to continue.
When your script executes, you’ll generate a dialog box for the user.
The command could also be used to echo an environment variable during the execution of any type of script. Take for example if you had wanted to display the code used by the user’s current prompt. The command xmessage $PS1 generates output based on this, though it would only work if the user executed this from a terminal window or allowed their file manager to do so.
Method 2: Creating Buttons with xmessage
Script programmers can use xmessage to create buttons for the user to select. Considering the following line:
xmessage “Can anybody hear me?” -buttons yes,no
Text to appear in the box got enclosed in quotes. The tack buttons command then features comma delimited button labels after it. Exit values equal 100 plus the number of the button tapped, so it’s possible to find out which one the user selected. You could create extra buttons beyond the basic two just like this:
xmessage “Remember this prompt?” -buttons Abort,Retry,Fail
It makes writing Bash and tcsh scripts easier for those who don’t want to work with a terminal.