As we know, we used the following syntax will to create a default script bash.
First line: signifies that is the bash script:
1 | #!/bin/bash |
Every line and everything after the # is treated as a comment:
1 | # this is a comment |
A shell variable may be assigned using the following syntax:
variable=value_of_variable
Example:
1 2 3 | $ a=10 $ echo $a 10 |
Try this command:
1 | $man bash |
1 2 3 4 5 6 7 8 9 | We'll see: ... Shell Variables The following variables are set by the shell: BASH Expands to the full file name used to invoke this instance of bash. BASH_ARGC ... and more |
Let’s try a simple script. First, we created file test.sh with this code:
1 2 3 4 5 | #!/bin/bash var1=$1 var2=$2 echo var1 $var1 echo var2 $var2 |
Let’s see some outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $ sh test.sh var1 var2 $ sh test.sh 11 var1 11 var2 $ sh test.sh 11 12 var1 11 var2 12 $ sh test.sh none 11 var1 none var2 11 $ sh test.sh '' 11 var1 var2 11 |
We’ll see how we work with Shell Variables.
1 2 3 4 5 6 | $ echo $SHELL /bin/bash $ echo $USER Cata $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/games |
Good. Let’s set one.
1 2 3 4 5 6 7 | TMOUT If set to a value greater than zero, TMOUT is treated as the default timeout for the read builtin. The select command termi- nates if input does not arrive after TMOUT seconds when input is coming from a terminal. In an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if input does not arrive. |
How set this variable?
First of all, the value of TMOUT…
1 2 | $ echo TMOUT TMOUT |
Now we will set this value to 10
1 | $ export TMOUT=10 |
It is 10? Let’s see:
1 2 | $ echo $TMOUT 10 |
Now the terminal will close in 10 seconds of inactivity.
This is the first part of the tutorial.