Korn shell Tutorials :Manipulating the variables

Korn shell Tutorials :Manipulating the variables
Removing something from a variable
Variables that contain a path can very easily be stripped of it: ${name##*/} gives you just the filename.
Or if one wants the path: ${name%/*}. % takes it away from the left and # from the right.
%% and ## take the longest possibility while % and # just take the shortest one.
Replacing a variable if it does not yet exits
If we wanted $foo or if not set 4 then: ${foo:-4} but it still remains unset. To change that we use:
${foo:=4}
Exiting and stating something if variable is not set
This is very important if our program relays on a certain vaiable: ${foo:?"foo not set!"}
Just check for the variable
${foo:+1} gives one if $foo is set, otherwise nothing.

No comments: