Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What linux shell command returns a part of a string? [duplicate]

I want to find a linux command that can return a part of the string. In most programming languages, it's the substr() function. Does bash have any command that can be used for this purpose. I want to be able to do something like this... substr "abcdefg" 2 3 - prints cde.


Subsequent similar question:

  • Extract substring in Bash
like image 380
Binny V A Avatar asked Oct 20 '08 18:10

Binny V A


People also ask

What is $2 in shell script?

$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.


1 Answers

If you are looking for a shell utility to do something like that, you can use the cut command.

To take your example, try:

echo "abcdefg" | cut -c3-5 

which yields

cde 

Where -cN-M tells the cut command to return columns N to M, inclusive.

like image 165
Toybuilder Avatar answered Sep 24 '22 16:09

Toybuilder