% here is called the pattern matching operator.
Quote from the book โLearning the Bash Shellโ:
The classic use of pattern matching operators is to remove path name components, such as directory prefixes and file name suffixes. With that in mind, here is an example that shows how all operators work. Assume the path variable has the value /home/cam/book/long.file.name ; then:
Expression Result Comments ${path##/*/} long.file.name
They can be difficult to remember, so here is a handy mnemonic device:
# matches the front because numeric characters precede numbers;% matches the rear because percent signs follow numbers.
In your particular case, 0 is an analogue of path in my example, so you should know this.
If $0 is equal to /home/chj/myfile.txt , then cd ${0%/*} will expand to cd /home/chj , that is, the "file" part will be deleted.
I understand your desire to ask this question, because it is too difficult to find the answer without spending several hours studying the Bash book.
source share