Positional parameters are the arguments given to your scripts when it is invoked. It could be from $1 to $N.
The variable $0 is the basename of the program as it was called.
⚠️ Note
When N consists of more than a single digit, it must be enclosed in braces like${N}.
Example:
#!/bin/bash
echo “My Name is $1”
echo “My favourite os is $2”
echo “My favourite color is $3”myscript Hamed linux green
# Hamed --> $1
# linux --> $2
# green --> $3The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
| Special Parameter | Description | Shell | Value |
|---|---|---|---|
| $# | Expands to the number of positional parameters in decimal. | my_script.sh 1 2 3 | 3 |
| $?* | Expands to the exit status of the most recently executed foreground pipeline. | echo "Hi!" echo $? | 0 (Because echo "Hi!" is successful) |
| $0 | Expands to the name of the shell or shell script. | my_script.sh 1 2 3 | my_script.sh |
| $@ | Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. | my_script.sh "hello world" | hello world (2 words) |
| "$@" | Expands to the positional parameters, without subsequent word splitting. | my_script.sh "hello world" | hello world (1 words) |
| $* | Exactly the same as $@ | my_script.sh "hello world" | hello world (2 words) |
| "$*" | Expands to the positional parameters, without subsequent word splitting separated by the first letter of the IFS variable. | IFS=, my_script.sh "hello world" | hello,world (1 words) |
read is a bash built-in command that reads a line from the standard input (or from the file descriptor) and split the line into words. The first word is assigned to the first name, the second one to the second name, and so on.
Syntax for read command:
read [options] [name...]
Example:
$ read $var1 $var2
Hello, World!$ echo $var1
$ echo $var2Output:
Hello,
World!| Option Name | Description |
|---|---|
| -p <prompt> | Outputs the prompt string before reading user input. |
| -s | Does not echo the user's input. |
| -t <time> | The command times out after the specified time in seconds. |
| -N <number> | Returns after reading the specified number of chars, ignoring the delimiter. |
| -n <number> | Limit the user's response to a maximum number of chars. |
Example:
#!/bin/bash
read -t 5 -p "Input your first name within 5 seconds: " name
read -n 2 -p "Input your age (max 2 digits): " age
read -s -N 5 -p "Enter your password (exactly 5 digits): " password
echo "$name, $age, $password" >> data.csvThe select command provides a dropdown
menu to select. The user may select an option from
the list of options.
It is also possible to provide a prompt using the
PS3 shell variable.
Syntax for the select command:
PS3="Please select an option: "
select variable in options ; do
<commands>
break
done
Example:
#!/bin/bash
PS3="What is your operating system?: "
select os in linux mac windows ; do
echo "The operating system is $os"
break
done