6.10.12

Bash notes_1

String operations
Goal: isolate something useful from a string, short or long, also multi-line.
Operators:
  • # delete from left to the first case of what follow
  • ## delete from left to the last case of what follow
  • % delete from right to the first case of what follow
  • %% delete from right to the last case of what follow
Example
x="this is a test string"
echo ${x#*is}
echo ${x##* }
echo ${x%test*}
echo ${x%% *}
prints
is a test string
string
this is a 
this

All clear?!?

Goal: Select substring from a string
How: ${string:starting_point:offset}
Estract the substring from starting_point position of string for a number of offset char.
Example
x="example string"
echo ${x:2:4}
echo ${x:8:1}
prints
ampl
s

Nessun commento:

Posta un commento