LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to manipulate string in script? (https://www.linuxquestions.org/questions/programming-9/how-to-manipulate-string-in-script-290417/)

ringerxyz 02-15-2005 03:26 AM

how to manipulate string in script?
 
for example:
if a string str ="abcde",and now I want get first 4 characters out from the string,
how should I do?

jlliagre 02-15-2005 03:33 AM

bash only way:
Code:

${str:0:4}
portable way:
Code:

expr "$str" : '\(.\{4\}\)'
The latter doesn't output anything if the wariable length is less than 4.

chrism01 02-17-2005 01:14 AM

Another portable method:

echo asdfgh|cut -c1-4
asdf


All times are GMT -5. The time now is 05:34 PM.