Bash-rarely-used-options: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
(Created page with "Suspect they will be version dependent, but maybe not. [https://stackoverflow.com/questions/4168371/how-can-i-remove-all-text-after-a-character-in-bash | Original link] <p...")
 
(No difference)

Latest revision as of 12:50, 15 October 2021

Suspect they will be version dependent, but maybe not.

| Original link

In Bash (and ksh, zsh, dash, etc.), you can use parameter expansion with % which will remove characters from the end of the string or # which will remove characters from the beginning of the string. If you use a single one of those characters, the smallest matching string will be removed. If you double the character, the longest will be removed.

$ a='hello:world'

$ b=${a%:*}
$ echo "$b"
hello

$ a='hello:world:of:tomorrow'

$ echo "${a%:*}"
hello:world:of

$ echo "${a%%:*}"
hello

$ echo "${a#*:}"
world:of:tomorrow

$ echo "${a##*:}"
tomorrow