Useful Bash Command Line Tips and Tricks Examples – Part 5

In this article, we will explore user input: for all those times you want to ask the user to ‘press enter to continue’, or to actually read a string of input and store it into a variable for later processing. We will also look at how to find manual pages for built-in commands which otherwise may not seem to be available.

In this tutorial you will learn:

  • Useful Bash command line tips, tricks and methods
  • How to interact with the Bash command line in an advanced manner
  • How to sharpen your Bash skills overall and become a more proficient Bash user

Useful Bash Command Line Tips and Tricks Examples - Part 5

Useful Bash Command Line Tips and Tricks Examples – Part 5

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems)
Conventions # – requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires linux-commands to be executed as a regular non-privileged user

Example 1: Requesting the user to press enter to continue

Have you ever written a script in which you wanted to ask the user to ‘press any key to continue’? Whereas asking the user to press any key may not be a smart idea – some users may press the power key 🙂 – requesting the user to press enter need not be hard from within a script:

$ cat test.sh 
#!/bin/bash
read -p 'Press enter to continue...'
echo 'Thank you for pressing enter... Please come again...'

The -p (prompt) option to read creates a textual prompt.

Let’s test this!

$ ./test.sh
Press enter to continue...
Thank you for pressing enter... Please come again...


I pressed enter after the Press enter to continue… and the script worked correctly.

Example 2: Reading input from a script

Let’s take example 1 a bi further and read an actual typed input into a variable:

$ cat test.sh 
#!/bin/bash
read -p 'Your input: ' VAR1
echo "Input given: ${VAR1}"

Here we again employ the use of -p to create a textual prompt, and we read the input into the variable VAR1. Let’s see if this works as expected:

$ ./test.sh 
Your input: I am typing here            
Input given: I am typing here

It works correctly. You may want to use read --help to read more Note that man read will not work, as this will bring you to the Linux programmer’s manual for performing a file read!

Reading the manual for read is highly recommend, as by default read will do some interesting word splitting. In our example this was hidden due to the fact that we specified only a single storage variable in our read command, but there is much more to learn, especially if you know how to work with the IFS variable, which indicates – to several Bash tools – what the separator needs to be, in combination with specifying multiple storage variables.

Example 3: Where’s that manpage?

As we saw in example 2, sometimes a man page seems to be missing or leading to a wrong result like in our man read example. However, this is not the case. The reason that some man pages do not seem to be available is that they are for built-in commands.

You can instead use the man builtins command to access a manual for all built-in commands.

Thus, if you want to access the full manual for, for example, read you can use man builtins and search for read there.

If you would like to learn more about Bash in general, have a look at the Useful Bash Command Line Tips and Tricks Examples series.

Conclusion

In this article, we explored input at the command line, retrieved from within a script. We looked at how to ask the user to press enter to continue, as well as actually reading in a string of input and storing it in a variable. We also hinted towards exploring read and IFS further, and finally we looked at how to find manpages for built-in commands. Enjoy!



Comments and Discussions
Linux Forum