skripting-section:skripting-grundlagen:bash-grundlagen

This is an old revision of the document!


Bash Grundlagen

To extract substrings from variables in bash, we can use the following syntax

${variable_name:offset:length}

Caveats:

If offset or length is negative, count from the end Negative values must be separated by a space away from the colon (:) to avoid being interpreted by bash as default value substitution. (See below for examples)

Get the file extension of a filename

$ filename=foobar.txt
$ echo ${filename: -4}
.txt

Get the file name without extension

$ filename=foobar.txt
$ echo ${filename:0: -4}
foobar
  • skripting-section/skripting-grundlagen/bash-grundlagen.1557155283.txt.gz
  • Last modified: 2019/05/06 17:08
  • by michael