shellscript filename or string header or tail



$ vi testfilename.sh
#!/bin/sh
testname="a1.b2.c3.d4.e5"
echo "testname="$testname
echo '${testname%%.*}'
echo "${testname%%. *}" #Take the head
echo '${testname%.*}'
echo "${testname%.*}" #Go to the tail
echo '${testname#*.}'
echo "${testname#*.}" #Go to the head
echo '${testname##*.}'
echo "${testname##*.}" #Take the tail

$ ./testfilename.sh 

testname=a1.b2.c3.d4.e5
${testname%%.*}
a1
${testname%.*}
a1.b2.c3.d4
${testname#*.}
b2.c3.d4.e5
${testname##*.}

e5

Simple way to understand:

The way to remember is: # is to remove the left side (# is on the left of $ on the keyboard), % is to remove the right side (% is on the right of $ on the keyboard), a single symbol is the smallest match; two symbols are the largest match

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324791006&siteId=291194637