May 23, 2021 UNIX Getting started
Shell performs an override when it encounters an expression that contains one or more special characters.
Here is an example in which a variable is replaced by its true value. At the same time, the ""
#!/bin/sh
a=10
echo -e "Value of a is $a \n"
This results in the following results. Here the -e option explains the backslash escape.
Value of a is 10
Here are the results without the -e option:
Value of a is 10\n
Here are the following escape sequences available for the echo command:
Escape | Description |
---|---|
\\ | Backslash |
\a | Alert (BEL) |
\b | Back to the bar |
\c | Suppress line changes |
\f | Change the page |
\n | Line change |
\r | Enter |
\t | Horizontal tabs |
\v | Vertical tabs |
By default, you can use the -E option to disable backslash interpretation.
You can use the -n option to disable line-changing insertion.
Command substitution: A mechanism through which the shell executes a given command and then overrides their output on the command line.
Command substitution is performed when the following command is given:
command
When you execute a command replacement make sure that you use inverse quotes instead of single quote characters.
Command substitution is typically the output variable used to assign a command. The following example demonstrates command replacement:
#!/bin/sh
DATE=`date`
echo "Date is $DATE"
USERS=`who | wc -l`
echo "Logged in user are $USERS"
UP=`date ; uptime`
echo "Uptime is $UP"
This results in the following:
Date is Thu Jul 2 03:59:57 MST 2009
Logged in user are 1
Uptime is Thu Jul 2 03:59:57 MST 2009
03:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15
Variable replacement enables the shell programmer to manipulate the value of the state-based variable.
The following table is all possible replacements:
Form | Description |
---|---|
${var} | Replaces the value of var |
${var:-word} | If the var is empty or has no assignment, word replaces var. The value of var does not change. |
${var:=word} | If the var is empty or not assigned, the var is assigned a value of word. |
${var:?message} | If the var is empty or not assigned, the message is compiled as a standard error. This detects whether the variable is correctly assigned. |
${var:+word} | If var is assigned, word replaces var. The value of var does not change. |
The following are examples of the various states used to illustrate the above alternatives:
#!/bin/sh
echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"
echo ${var:="Variable is not set"}
echo "2 - Value of var is ${var}"
unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var"
var="Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"
echo ${var:?"Print this message"}
echo "5 - Value of var is ${var}"
This results in the following:
Variable is not set
1 - Value of var is
Variable is not set
2 - Value of var is Variable is not set
3 - Value of var is
This is default value
4 - Value of var is Prefix
Prefix
5 - Value of var is Prefix