Strings are quite basic in any scripting or programming languages.
If a journey
to a thousand miles starts with a single step, in the programming world the
journey starts with a string called “Hello World” and beyond “Hello World” pseudocode
and algorithm will keep the journey going.
Strings in
scripting or programming are just group of characters.
So, if
string is a group of characters then it can be slice by character or a sub-string
can be taken from the string.
To slice a
string in Bash, a syntax which is part of the Bash library can be used.
Syntax is:
${string_to_be_sliced:slice_start_position:slice_end_position}
Example
string: slicethestring=“Hello Algorithm World”
The string
is stored in a variable: slicethestring
To get the
sub-string “Algorithm World”.
Code will
be: echo ${slicethestring:6:23} #start
the slice at position 6 till position 23
Another way
to slice the string from a specified start position till the end; is to tell Bash
the start position and just omit the end position.
Or in layman
terms tell Bash to start at a specified position but don’t tell Bash when to
end, in this way Bash will automatically get all the characters until the last
position of the string.
Here’s the
code: echo ${slicethestring:6} #this
will concatenate until the end of the string but don’t put the “:” after the first position or else
there will be an error because Bash will expect an input following the colon “:”.
Both codes
will display: Algorithm World
Here’s the
screen shot of the script and the output:
Script:
Sample output:
Cheers..till next time!
Heaven's Dew Fall Prayer app for Android :
https://play.google.com/store/apps/details?id=com.myrosaryapp
Comments
Post a Comment