Trimming suffix or perfix of a string in Shell

by Forrest Sheng Bao

How to trim/truncate a string in Linux? Especially when it is a path or file name?

See this demo below.


echo ${test%_label} # the percentage sign means from the end

echo ${test#?/*/} # the pound sign means from the start 

Save it as a Shell script (e.g., and test it on your Shell:

$ sh 

Got the idea?

This is very simple string matching, where question mark and asterisk have their normal meanings in UNIX regular expression.

No comments: