Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10033
  • Last Modified:

how to truncate a string in bash shell script?

I would like to know how to truncate a string in bash shell script? Thanks.
0
pink21
Asked:
pink21
1 Solution
 
ecwCommented:
To strip off a known part,
   ${var%pattern}        # shortest match
or ${var%%pattern}       # longest match
Where pattern is a valid filename expansion pattern eg.
  var=main.c.old
  echo ${var%.*} ${var%%.*}
While output
  main.c main

To get the first (for example 10) chars of a var
  var=`echo $var | cut -c -10`
or var =`expr substr 1 10 $var`

0
 
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: ecw {http:#8203912}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now