Special characters in the Bash Shell

Source: Internet
Author: User

What are the special characters in the bash shell? Some special characters in the bash shell do not represent the meaning of their characters themselves, but have another meaning, called: meta-meaning.

    • 1. Characters: #

Represents a comment, if one line starts with #, then it means that the line will not be executed, just a single line of explanatory notes.

For example:

# This is comments

This is a line of comments.


But here's the exception, which is the first line of the script that we often see:

#! /bin/bash

This instructs the interpreter of a script that the script is interpreted by/bin/bash.


From # to start, the following character is a comment until the line ends. Here are some examples:

echo "The # here does not begin a comment." Echo ' The # here does not begin a comment. '

The characters within the reference are output as intended.

echo the \# here does not begin a comment.

This uses the \ to escape the #, that is, the # is output.

echo the # here begins a comment.

This is miserable, truncated, only output: the

echo ${path#*:}

This is a parameter substitution, not an annotation character.

echo $ ((2#101011))

This is a cardinality indicator, not an annotation, indicating that the following number is binary.


    • 2. Character:;

The semicolon [semicolon], as the delimiter for the command, describes the end of a command. With semicolons, you can write multiple commands on one line, which we often see.

For example:

hdfeel$ echo Hello; Echo world;helloworldhdfeel$


3. Character:;;

Two semicolons together [double semicolon], this is only used in case statements:

Case "$variable" in ABC) echo "\ $variable = abc";; XYZ) echo "\ $variable = xyz";; Esac


    • 4. Characters: "

Double quotation marks [double quote] denote partial references [partial quoting], in most cases, the characters inside the double quotation marks are output as is, but the variables in them are replaced. Like what:

hdfeel$ filename= "A.txt" hdfeel$ echo "The file name is $fileName" hdfeel$ the file name is A.txt

That's probably what we need. If you want to output as-is, use single quotation marks, or escape with a backslash \.

echo "The file name is \ $fileName" Echo ' The file name is $fileName '


    • 5. Characters: '

Single quotation marks [quote], which represent the full reference [fully quoting], the characters in the quotation marks are all output as is, even if there are variables that do not replace them accordingly.

The example above is not repeated.


    • 6. Characters:,

comma [comma operator], connecting 2 equations, but only the value of the last calculation is returned.

hdfeel$ let "t2 = ((a = 9, 15/3))"


The above statement assigns a value of 9 and divides 15 by 3 for the result 5, which is assigned to T2.

In addition, the comma operator can also concatenate strings:

For file in/{,usr/}bin/*calc# ^ in the/bin and/usr/bin directories, find all executables that end with "Calc" #+ do if [-X "$file"] then echo $file fidone#/bin/ipcalc#/usr/bin/kcalc#/usr/bin/oidcalc#/usr/bin/oocalc


    • 7. Characters:

A backslash [backslash] is a code break [escape], a reference mechanism for a single character that changes its meaning by referring to a character.

Like what:

hdfeel$ echo "\" "; Output double quotes hdfeel$ "hdfeel$ x=abchdfeel$ echo" $x "//Output variable value hdfeel$ abchdfeel$ echo" \ $x "//Output hdfeel$ $x


    • 8. Characters: '

Command substitutions, which are the characters below the tilde in the upper-left corner of the keyboard, do not look wrong, not single quotes Oh,<code> ' </code> Middle put an executable command <code> ' </code>, this symbol is also called anti-quote.

For a small example:

#! /bin/bashinfile= "Example.txt" maxlen=12#while read line; Do len= ' echo $line |    The awk ' {print length ($)} '//subcommand is placed inside the counter-quotes to execute. If [$len-lt $maxLen]; Then echo $line Fidone < $inFile

The above program prints lines that are less than 12 characters long.


    • 9. Colon:

The colon [colon], called the null command, is not executed, and the return value is true, which is 0, which is a built-in command of bash.

The following 2 snippet of code treats the colon as a condition of judgment:

While:d o operation-1 operation-2 ... operation-ndone

The same functionality as the above code implementation

While Truedo operation-1 operation-2 ... operation-ndone

Sometimes it is also used as a placeholder:

If Conditionthen: # do nothing and branch Aheadelse # Or Else ... take-some-actionfi


Colon: Also used as a domain delimiter, you can see it in the/etc/passwd and $path variables:

Shells hdfeel$ echo $PATH/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinshells hdfeel$


The most dejected, colon this thing can also do function name, if want to let the code to make people dizzy, so doing

:() {echo "The name of this function was" $FUNCNAME "" # Why use a colon as a function name? # It's a obfuscating your code.}:# the name of this function is:

Of course inside the function, it can make a placeholder, so the function is not an empty function.

Not_empty () {:} # Contains A: (null command), and so are not empty.


Here's a more practical one:

{A.. Z} generates all characters from A to Z, and of course it can be a number, for example: {1..9}

Experience this Code

echo {A.. Z} # A b c d e F g h i j k l m n o p q R S t u v w x y z# echoes characters between A and Z.echo {0..3} # 0 1 2 3# echoes Characters between 0 and 3.base64_charset= ({A). Z} {A.. Z} {0..9} +/=) # Initializing an array, using extended brace expansion.# from Vladz ' s "base64.sh" example script.


Some of the special characters in the Bash shell are explained above, and there are more and more detailed introductions in the <a Advanced Bash-scripting Guide, which are interesting to refer to.


Original: http://www.hdfeel.net/2015/01/bash-shell-special-character/


This article is from the "Hdfeel It Perspective" blog, so be sure to keep this source http://hdfeel.blog.51cto.com/9805995/1606620

Special characters in the Bash 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.