Various strange symbols in the Linux shell

Source: Internet
Author: User

There was a time when the shell was not written, and today I occasionally see a shell file content. There are some special usage, only feel the impression but can not say the specific function, and then checked. By the way record down, prepare for later reference.

First, learn something from a script house article (http://www.jb51.net/article/60326.htm) and say that the various parentheses in the shell can be listed in the following ways:

    1. ${var}

    2. $ (CMD)

    3. ${var:-string},${var:+string},${var:=string},${var:?string}

    4. ${var%pattern},${var%%pattern},${var#pattern},${var# #pattern}

I deliberately jumped two, personally think that it is very common, and not much use.

${var} is only a $var variant, but the former is more clearly marked with the variable name than the latter. From the script I see, many people use the form of ${var}.


$ (CMD) is used to execute CMD, which returns the result. There is also an older version of ' cmd ' (using anti-quotes, the one below the ESC), remember where to see that it is literally too low to distinguish the string, so gradually discarded. Use the $ (CMD) Form instead. However, I see the script ' cmd ' in the form of more (probably because it was previously retained).


${var:-string},${var:+string},${var:=string},${var:?string} This series, which can be called conditional assignment. Only the conditions and behavior are different.

${var:-string} returns a string when Var is empty, but the Var variable itself is still empty

${var:=string} When Var is empty, a string is returned, but the Var variable is also assigned a string (feeling a bit like the default parameter of the function)

${var:+string} returns a string when Var is not empty, but the Var variable itself does not change. When Var is empty, the expression returns an empty

${var:?string} When Var is empty, returns a string to stderr and ends the shell


${var%pattern},${var%%pattern},${var#pattern},${var# #pattern} This series, which is used to remove specific parts of a variable according to pattern.

${var%pattern},${var%%patten} is removed from the right, and ${var#pattern},${var# #patten} is removed from the left. ${var%pattern},${var#patten} indicates that the ${var%%pattern},{var# #patten} is removed as long as the shortest possible. If you do not use wildcards, there is no shortest and longest concept.

The common use is to get the file name and the path of the files from a full path.

Path= $HOME/public/foo.sh

${path##*/} will return the filename foo.sh, removing the longest match found on the left by */

${path%/*} will return the directory where the file is located $home/public (the actual returned value $home will be replaced with the real path), removing the shortest match on the right


The other part is the various pre-defined variables that begin with $.

$$ the PID that the script runs, typically used to generate log

$? Script Execution Results

$ #表示脚本参数的个数

"$*" puts all the script arguments into a string, separating each parameter with IFS. For example "1 2 3"

[email protected] puts all the script parameters in a string group. e.g. "1" "2" "3"

$n mark the nth script parameter (0<=n<=9), parameters greater than 9 use ${n} to obtain a


Finally remember the variable nesting, this is the need to change the inner layer of $!.

Refer to the example in "Linux command line and Shell Scripting Bible".

You want to output the last parameter of the script. The last parameter ordinal can be obtained by $ #获取, and any one parameter can be acquired by ${n}. But in {} is not able to be used with $, need to use! Replace. So ${$#} to become ${!#} to get the correct output. Nested variables are generally used in the form of!var in the inner layer


Write it here first, and then continue to add it back.

This article is from the "7thExpress" blog, be sure to keep this source http://7thexpress.blog.51cto.com/8005563/1763233

Various strange symbols in the Linux 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.