SHELL Variable Summary

Source: Internet
Author: User

Positional variables

$ A Gets the file name of the current user script contains the path

$n gets the nth parameter of the currently executing shell script if n is greater than 9 in curly braces ${10}

$# gets the number of arguments for the currently executing shell script

$? Check if the last command is successful, if 0 is returned successfully

0 successes

2 Permission denied

125 script Run Error system related

126 Command not executed

127 No commands to execute

>128 command is forcibly stopped by the system

$$ Process number

BaseName the name of the script

DirName $ $ Script Path

Character matching

${#string} Returns the length of the $string

${string:position} in string, starting from position $position to extract string

${string:position:length} in string, starting from position $position to extract substring of length $length

${string##} removed from the beginning

${string%substring} removed from the end

${STRING/SUBSTRING/REPLAC} replace $replac first match replace $substring

${STRING/%SUBSTRING/REPLAC} replaces $replac first match replacement $substring from the end

${string/#SUBSTRING/replac} replaces $replac first match replacement $substring from the beginning

Wd= "I am a Boy"

echo ${#WD} print string length

Echo ${wd:2} returns after the first character

echo ${wd}|wc-m can also print character lengths

Echo ${wd:0:3} Starts from the No. 0 and takes three

echo ${wd}|cut-c 1-5 take 1-5

Echo ${wd#i} removes the shortest character I from the beginning

echo ${wd# #I} remove the longest character from the beginning I

echo ${wd/i Am/you is} first match replace

Echo ${wd/#} matches from the beginning

Echo ${wd/%} matches from the end


Defining variables


${value:-word} detects an empty variable and prints it if $string is not defined position



["$t"! = "UNSET"] && export cron_value= "$t"

Find ${path:=/tmp/}-name *.tar.gz-type-f |xargs rm-rf


${value:=word} If the variable is not defined, the value is assigned to the variable.


${value:? " Message "} If the variable name exists and is not NULL, the value of the variable is returned. Otherwise, the variable name is displayed: Message


${value:+word} returns Word if the variable name exists and is not NULL, otherwise null is used to detect whether the variable is defined


${value-word} If the variable does not exist in Word instead of vsftpd the following is useful to

SHELL Variable Summary

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: 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.