Substitution is what.
Shell when it encounters an expression that contains one or more special characters for substitution. Example:
The following example enables printing to replace the value of the variable with its value. and "" is a new line to replace:
#!/bin/sh
a=10
echo -e "Value of a is $a
"
This will produce the following results. Here the-e option can explain the backslash escape.
Value of A is 10
Here is the result without the-e option:
Value of A is 10
Here the echo command can be used in the following escape sequences:
Escape
Describe
\
Backslash
A
Alert (BEL)
Backspace
C
Suppress trailing newline
F
Form Feed
New Line
Carriage return
Horizontal tab
V
Vertical tab
You can use the-E option to disable the interpretation of backslash escape (default).
You can use the-n option to disable inserting new rows. Command replacement:
Command replaces the mechanism executed by the shell, a given set of commands, and then replaces their output in the command. Grammar
When a command replacement is executed, it is given the following:
' Command '
When you perform a command substitution, make sure that you are using an inverted quote, not a single quote character. Example:
Command substitution is typically assigned to a variable with the output of one command. The following example shows the command substitution:
This will produce the following results:
#!/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"
Variable substitution:
Variable substitution allows the shell programmer to manipulate the value of the variable, depending on its state.
Here are all possible replacements for the following table:
Format
Describe
${var}
Substitue the value of var.
${var:-word}
If var is null or unset, the word is substituted for var. The value of Var does not change.
${var:=word}
If var is null or unset, the Var is set to the value of Word.
${var:?message}
If var is null or unset, the message was printed to standard error. This checks that variables are set correctly.
${var:+word}
If var is set, Word was substituted for var. The value of Var does not change. Example:
The following example shows various states, the above substitution:
#!/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}"
Metacharacters
The UNIX shell provides a variety of meta characters that have special meaning, while exploiting them in any shell script and leading to terminating a word unless referenced.
For example:? Matches a single system character, while listing the directories in the file and * matches multiple characters. The following is a list Shell special character (also known as a metacharacters):
* ? [ ] ' " $ ; & () | ^ < > New-line Space tab
It is associated with characters that might have been referenced before (for example, placing itself). Example:
The following example shows how to print a * or a?:
#!/bin/sh
echo Hello; Word
This will produce the following results.
#!/bin/sh
echo Hello; Word
Shell returned 127
Now, let's try using the quoted character:
#!/bin/sh
Echo Hello; Word
This will produce the following results:
Hello; Word
The $ symbol is a meta character, so it must be referenced to avoid special handling:
#!/bin/sh
echo "I have $1200"
This will produce the following results:
I have $1200
Is the following four forms of reference:
Reference
Describe
Single quote
All special characters between quotes lose their special.
Double Quote
Most special characters between these quotes lose their special meaning and these exceptions:
· $
· `
· $
· '
· "
· \
Backslash
Any character immediately following of the backslash loses its special meaning.
Back Quote
Anything in between back quotes would is treated as a command and would be executed. Single quotation mark:
Consider the echo command, which contains a number of special shell characters:
Echo <-$1500.**>; (update?) [Y|n]
The backslash that will be in front of each special character is cumbersome, making the line difficult to read:
Echo <-$1500.**>; (update?) [Y|n]
There is a simple way to refer to a large group of characters. String to start and end a single quotation mark ('):
Echo ' <-$1500.**>; (update?) [Y|n] '
Any character within a single quotation mark is a backslash, as if it were in front of each character. So, now this echo command will display correctly.
If you want to output a single quote within a string, you should not use the entire string within the single quotation mark, instead of the pair, using the backslash () as follows:
Echo ' It ' s Shell programming '
Double quotes:
Try executing the following shell script. This shell script uses single quotes:
Var=zara
Echo ' $VAR owes <-$1500.**>; [As of (' Date +%m/%d ']] '
This produces the following output results:
$VAR owes <-$1500.**>; [As of (' Date +%m/%d ']]
So that's not what you want to show. It is clear that single quotes prevent variable substitution. If the value of the variable you want to replace and the inverted comma work as expected, then you need to order in double quotes as follows:
Var=zara
echo "$VAR owes <-$1500.**>; [As of (' Date +%m/%d ']] "
This will produce the following results:
ZARA owes <-$1500.**>; [As of (07/02)]
Double quotes take away the special meaning of all characters except the following:
· $ parameter substitution.
· The inverted quotation mark used for command substitution.
· $ to make literal dollar sign.
· ' Make the literal inverted quote.
· "Enables embedded double quotes.
· \ enable embedded backslash.
· All other characters are text (not specified).
Any character within a single quotation mark is a backslash, as if it were in front of each character. So, now this echo command will display correctly.
If you want to output a single quotation mark within a string, you should not use the entire string within the single quotation mark instead of the backslash () as follows:
Echo ' It ' s Shell programming '
Reverse quotation marks:
Any shell command between the inverted quotes will execute the command syntax
Here is a simple syntax to put the inverted quotes between any shell commands: example:
Var= ' Command '
Example:
Following the execution of the date command, the resulting results are stored in the DATA variable.
Date= ' Date '
echo "Current Date: $DATE"
This produces the following output results:
Current Date:thu June 2 05:28:45 MST 2009
Most Unix system commands return to your terminal from the output of the input and send from the terminal. A command usually calls a standard input from a place, and by default, this happens to be your terminal read input. Similarly, a command usually writes its output to the standard output, which is also by default from your terminal. Output redirection:
The output typically used for standard output commands can easily be transferred to a file instead. This ability is called output redirection:
If the mark > file attaches any command, usually writes its output to the standard output, the output of the command will be written to the file instead of your terminal:
Check to use the WHO command to redirect the full output to the user file.
$ who > users
Please note that no output appears in the terminal. This is because the output has been redirected to the specified file from the default standard output device (terminal). If you want to check the user's file, then complete the content:
$ cat users
oko tty01 Sep 12 07:30
ai tty15 Sep 12 13:32
ruth tty21 Sep 12 10:10
pat tty24 Sep 12 13:07
steve tty25 Sep 12 13:03
$
If the command output is redirected to a file that already contains some data, the data will be lost. Consider this example:
$ echo line 1 > users
$ cat users
line 1
$
You can use the >> operator to attach the output to an existing file as follows:
$ echo line 2 >> users
$ cat users
line 1
line 2
$
Input redirection:
Just as the output of a command can be redirected to a file, you can enter a command to redirect from the file. As not a character > for output redirection, less than character < is used to redirect the input of a command.
Usually required by the commands they input from the standard input can have their own in this way from file input redirection. For example, the number of users in the file generated above can be calculated by executing the following command:
$ wc -l users
2 users
$
Here, it produces 2 rows of output. The number of rows in the file that can be counted WC commands the standard input of the redirected user from the file:
$ wc -l < users
2
$
Note that there is a difference in the output produced by the two-type WC command. In the first case, the user name of the file lists the number of rows, in the second case, it is not.
In the first case, the WC knows that it is the user who reads input from the file. In the second case, it only knows that it is reading input from the standard input, so it does not display the file name. Here Document:
Here document is used to input redirects to an interactive shell script or program.
In a shell script, we can run an interactive program that does not require user action, by providing the input required by an interactive or interactive shell script.
The general form of the file here is:
command << delimiter
document
delimiter
Here the shell interpretation << operation instruction reads the input until it finds contains the specified separator line. The line delimiter for all input lines, and then the command to enter the standard input.
The Terminator tells the shell that the file is finished here. Without it, the shell keeps reading the input. The delimiter must be a word that does not contain spaces or tabs.
The following is the total number of rows that the input command wc-1 to count:
$wc -l << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
3
$
You can print multiple lines in heredocument, using your script as follows:
#!/bin/sh
cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
This will produce the following results:
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
The following script runs the input and save file Test.txt in a session vi text editor.
#!/bin/sh
filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
EndOfCommands
If you run this script and use Vim as VI, you will most likely see output similar to the following:
$ sh test.sh
Vim: Warning: Input is not from a terminal
$
After you run the script, you should see the following additions to the file Test.txt:
$ cat test.txt
This file was created automatically from
a shell script
$
Discard output:
Sometimes you need to execute commands, but do not want to display the output on the screen. In this case, the output that can be discarded is redirected to the file/dev/null:
$ command >/dev/null
Here command is the name of the order to be executed. File/dev/null is a special file that automatically discards all of its input.
To discard both output and error output from a command, use standard redirects to stdout to stderr redirects:
$ command >/dev/null 2>&1
Here, 2 represent stderr and 1 represent stdout. You can display a message to stderr to the STDERR redirect standard input to the following:
$ echo Message 1>&2
REDIRECT command:
The following is a command that allows you to use a complete list of redirects:
Command
Describe
PGM > File
Output of PGM is redirected to file
PGM < file
Program PGM reads it input from file.
PGM >> File
Output of PGM is appended to file.
n > File
Output from stream and descriptor n redirected to file.
n >> File
Output from stream and descriptor n appended to file.
N >& m
The Merge output from stream n with Stream m.
N <& m
Merge input from stream n with Stream m.
<< tag
Standard input comes from this through next tag at start of.
|
Takes output from one program, or process, and sends it to another.
Note that the file descriptor 0 is normal standard input (STDIN), 1 is standard output (STDOUT), and standard error output (STDERR).
from:http://www.yiibai.com/shell/what_is_shell.html#