Question: What are special variable in unix?
Special variable are those variable which are reserved by unix shell. For Example $$ is special variable and it return the current process id.
Question: List the special variable?
- $$: Give the current process ID.
- $0: File name of current script.
- $n: $1 will give first argument, $2 will give 2nd argument.
- $#: The number of arguments supplied to a script.
- $*: Return all the arguments.
- $?: The exit status of the last command executed.
- $!: Process number of last command
Question: Can we define array variables?
Yes, we can define.
NAME01="Name 1" NAME02="Name 2" NAME03="Name 3" NAME04="Name 4" NAME05="Name 5"
Question: How to define array variable and use them?
Define Array variable
NAME01="Name 1" NAME02="Name 2" NAME03="Name 3" NAME04="Name 4" NAME05="Name 5"
Use single variable
echo $NAME[2];
Print each and every variable
for i in NAME01 NAME02 NAME03 NAME04 NAME05 do echo $i; done
Question: How to print array in single line?
echo ${NAME[*]};
Question: How to use Relational Operators?
- -eq: Equal
- -ne: Not Equal
- -gt: Greater than
- -lt: Less than
- -ge: Greater than OR Equal to
- -le: Less than OR Equal to
Question: How to use Boolean Operators?
- !: Not
- -o: OR
- -a: And
Question: Give an example of while?
a=1 while [ $a -lt 10 ] do echo $a if [ $a -eq 5 ] then break fi a=`expr $a + 1` done
Question: What is Substitution?
The shell performs substitution when it encounters an expression that contains one or more special characters.
Question: Give few example of Variable Substitution?
Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.
Examples
${var}
${var:-word}If var is null, word is substituted for var.
${var:=word}If var is null, var is set to the value of word.
${var:?message}If var is null, message is printed to standard error.
${var:+word}If var is set, word is substituted for var.
Question: What are the Metacharacters?
metacharacters have special meaning while using them in any Shell Script and causes termination of a word unless quoted.
Following are the meta characters.
* ? [ ] ' " \ $ ; ( ) | ^