During the execution of a script you can request to the user to insert some data through the
read command. This command, similarly to
scanf in C, won't visualize any new message, but will wait for the user to insert a variable and press the
Enter key.
This is an example:
#!/bin/bash
# This script requests to the user an input from the keyboard and then visualizes it.
# The -n options avoids to wrap.
echo -n "Write your name [Enter]: "
read user_name
echo "Hello $user_name!"
Command line parameters
The command line parameters are variables which are passed to the script when it is launched from the command line. Inside the script these parameters can be referred as following:
Variable | Meaning |
$# | It contains the total number of passed parameters |
$num_param | Contains the value of the num_param parameter |
$? | It contains the result of the last executed command |
$0 | Contains the name of the script |
$* | Contains all the ordered parameters |
"$@" | Contains all the ordered parameters with double quotes |
This is an example:
#!/bin/bash
# This script shows how to recall in the code the values
# passed to the script as command line parameters
clear
echo "The name of this script is: $0"
echo "You inserted $# parameters, which in order are: $*"
echo "The first one is: $1"
echo "The second one is: $2"
echo "The last executed command returned: $?"
echo