Single quotes, double quotes, and backslashes in the shell

Source: Internet
Author: User
Tags echo date


Requirements:SSH remotely modifies the file, the variable is taken from the file, and the single and double quotes in the shell script that are inserted into the remote server's file are the same as the delimiter of the string, not the delimiter of the character. Single quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns within the quotation marks are no exception, but single quotes cannot appear in the string. (Note that it is all, except that the single quote itself cannot appear in it). Double quotation marks are used to keep the literal value of all characters in quotation marks (carriage return is no exception), except in the following cases:
    • $ plus variable name to take the value of the variable
    • Anti-quotation marks still indicate command substitution
    • \$ represents the literal value of $
    • The literal value of \ ' representation '
    • The literal value of \ "represents"
    • \ \ denotes the literal value of \
    • In addition to the above, there is no special meaning in front of other characters, only the literal value.
From multiple stickers on the web, I've been a mistake, overestimating the shell's processing logic for quotation marks, the first quotation marks appear, to the second quotation marks are counted as a pair, and will not be wise to judge the fact that the first and fourth quotation marks are a pair. 1. Single quotation markSingle quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns within the quotation marks are no exception, but the single quotation mark cannot appear in the string when the shell encounters the first single quotation mark, it ignores any special characters that are followed by the second single quotation mark 2. Double quotation marksIn double quotes, symbols that are not ignored: ', $, \ 3, anti-quoteCommand substitution refers to the ability of the shell to interpolate the standard output of a command anywhere on a command line. There are two ways to make command substitutions in the shell: put the shell command in reverse quotation marks or $ (...). Structure enclosed, where, $ (...) Formats are supported by POSIX standards and are also beneficial for nesting. The function of the anti-quote and $ () is to replace the command with an inverse quotation mark or a string in $ () as a command to execute, which we often use in shell programming to assign the execution result of the system command to a variable but cannot reference the anti-quotation mark in the inverted quotation mark, and the back quotation mark in $ () [email Protected] bin]# s=$ (echo date) [[email protected] bin]# echo $sdate [[email protected] bin]# s=$ (echo ' date ') [email Protec Ted] bin]# Echo $sSat 11:35:20 CST 2017 4. Back slashBackslashes are generally used as escape characters, or escape characters, and Linux if Echo wants the escape character to work, use the-e option, and the escape character uses the double quotation mark echo-e "\ n" backslash, that is, when the backslash is used for the last character in a row, The shell takes the backslash as a continuation line, which is often used when entering long commands in several lines. Example:1. Single quotation mark in single quotation mark
 
[[email protected] bin]# echo $‘\‘‘
‘
[[email protected] bin]# echo -e ‘\047‘
‘
2. Single quotation mark in processing file
[[email protected] package]# echo "mmm‘sss" | sed s#\‘#\\\\047#g mmm\047sss
[[email protected] package]# echo "mmm‘sss" | sed ‘s/‘"‘"/"\\\047"‘/g‘ mmm\047sss
[[email protected] package]# echo "mmm‘sss" | sed "s/‘/\\\\047/g" mmm\047sss
3. Can I print variables in single quotation marks?
 
[[email protected] ~]# echo ‘"$key"‘
"$key"
[[email protected] ~]# echo ‘‘$key‘‘
abc
4. Remote processing File command: SSH $ip "echo" $key = $value ' >> file ' or ssh $ip ' sed-i ' s/^ $key. */$key = $value/g ' >> file ' supplement: $v The alue variable contains any character that is discussed: sed or echo in SSH using single or double quotation marks


Single quotes, double quotes, and backslashes in the shell


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.