The difference between single and double quotes in bash

Source: Internet
Author: User
The difference between single and double quotation marks

Single quote: Must be used in pairs, it can protect all characters from being translated. such as variable $, and odd number of single quotes do the same, an even number of single quotes = 1 double quotes
Double quotation marks: must appear in pairs, it can protect some meta-characters are not translated, but allow variables and command substitution, and even a number of single quotation marks the same effect
The backslash \:shell also does not explain the characters after the escape character, ' $ ' and ' \$1 '.
Single and double quotation marks can protect each other

Example 1:

$ cat test.sh

#!/bin/bash

echo "$" =$1 "$" =$2
Echo ' $ ' =$1 ' =$2

$./test.sh Hello World
Hello=hello World=world
$1=hello $2=world

Example 2:

#!/bin/bash

Echo ' $ ' =$1
Echo ' $ ' = ' $ '
Echo ' $ ' = ' $ '//a double quote
Echo ' $ ' = $
Echo ' $ ' = '$ '
Echo ' $ ' = ' $'//two single quotes

Example 3:

Backslashes in single quotes are not translated
$echo ' \ \ '
\\

Example 4:

Single quote protected double quotation mark
$echo ' mother yelled, ' time to eat! ' ‘
Mother yelled, "Time to eat!"

Example 5:

Double quotation mark Protection single quotation mark
$echo "Hi, I ' m glad to mee you"
Hi, I ' m glad to meet

The difference between single and double quotes in bash

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.