Shell replacement

Shell does the magic when it encounters an expression that contains one or more special characters.

What is the alternative (Substitution) in Unix / Linux?

Shell does the magic when it encounters an expression that contains one or more special characters.

For example:

Here is an example, while printing a value of the variable, it will be replaced by the value of the variable. At the same time "n" is replaced by a new line.

 #! / bin / sh a = 10 echo - e "Value of a is $ an" 

It will produce the result as shown below. Here, the -e function will allow to compile the backslash escape sequence.

 Value of a is 10 

Without the -e function, it produces the following result:

 Value of a is 10 n 

Here are the escape sequences that can be used by echo command:

Escape sequence Description Backslash a Warning sign (BEL) b Backspace key c line cross-section f form feed n New line r Return t Tab horizontal v Vertical tab

You can use the -E function to prevent translation of cross braces (default).

You can use the -n function to disallow insertion of new lines.

Command substitution (Command Substitution) in Unix / Linux

Command substitution is a technique by which Shell executes a set of provided commands and then replaces the results of the commands.

Syntax

Command substitution is performed when a command is provided as follows:

 `command` 

When executing a command replacement, make sure that you are using 2 single-type quotation marks (`), not" ".

For example

Command substitution is normally used to assign the output of a command to a variable. Each of the following examples explains how to use the command substitution.

 #! / 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" 

It will produce the following result:

 Date is Thu Jul 2 03 : 59 : 57 2009 MST Logged in user are 1 Uptime is Thu Jul 2 03 : 59 : 57 2009 MST 03 : 59 : 57 up 20 days , 14 : 03 , 1 user , load avg : 0.13 , 0.07 , 0.15 

Variable Substitution in Unix / Linux

Variable substitution allows Shell programmers to manipulate the value of the variable based on its state.

Below is a table for all possible alternatives:

Sample Description $ {var} Replace the value of var . $ {var: -word} If var is null or unset, word is replaced for var . The value of var does not change. $ {var: = word} If var is null or unset, var is set to the value of word . $ {var:? message} If var is null or unset, the message printed is an error. This command checks to see if the variables are set correctly. $ {var: + word} If var is set, word is replaced for var. The value of var does not change.

For example:

The following is an example to illustrate the various states of the replacement above:

 #! / 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}" 

It will produce the following result:

 Variable is không đặt 1 - Value of var là Variable không đặt 2 - Value of var là Variable không đặt 3 - Value of var này là mặc định 4 

According to Tutorialspoint

Previous article: Loop control in Unix / Linux

Next article: Techniques cited in Unix / Linux

5 ★ | 1 Vote | 👨 208 Views
« PREV POST
NEXT POST »