Delete first character from a string stored in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete first character from a string stored in a variable
# 1  
Old 06-18-2010
Delete first character from a string stored in a variable

Hallo!

Example.

Code:
#!/bin/bash

BACKUP_DIR=/home/userx/backups/evolution

echo $BACKUP_DIR

# delete the first character from the string 

BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-)

echo $BACKUP_DIR

Quote:
/home/userx/backups/evolution
home/userx/backups/evolution
It works. It does want I want, delete the first character from string in the variable.

My questions, could this be done in another way (more "sophisticated")?

Thanks, for feedback!
# 2  
Old 06-18-2010
Yes:

Code:
${BACKUP_DIR#?}

# 3  
Old 06-18-2010
Try using SED
Code:
echo $BACKUP_DIR |sed 's/^.//'

# 4  
Old 06-18-2010
Code:
DIR=${DIR:1:${#DIR}}

# 5  
Old 06-18-2010
Hi posix, imho that is not an improvement to the OP's solution with cut. Radoulov's solution is elegant in that it does not need a pipe or an external program, nor a subshell plus it works in any POSIX compliant shell. I don't think it can be beat...
# 6  
Old 06-18-2010
@Scrutinizer- You are right code written by Scrutinizer is much more improvement one than me, i really appreciate it. Also i learnt a lot more things form this forum.
Thanks a lot to all moderator, author and staff.
# 7  
Old 06-18-2010
BIG THANKS, TO ALL FOR HELP!

Lets say "radoulov" 10 points! OK! Smilie

Last edited by linuxinho; 06-18-2010 at 07:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem... (1 Reply)
Discussion started by: dellanicholson
1 Replies

2. UNIX for Beginners Questions & Answers

Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that. Currently I tried this: match = grep "Hello" file | grep -v "Hi" file But that's not... (2 Replies)
Discussion started by: pavan
2 Replies

3. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

how can I check the first character of each variable stored in array

variables are stored in this format D0000, D12345, S1234, D12345 | KestrelPrdV2_0.build.177 - svn 141115 tag as V2.0.0.14 D0000, D12345, S1234, D12345 My question is how can I check the first character of each variable stored in array.It should be start with D or S. Please help... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

5. Shell Programming and Scripting

How to change last character in string with a variable value.

Hey Guys, I have text such as this. 28003,ALCORN,2 28009,BENTON,2 28013,CALHOUN,2 28017,CHICKASAW,2 47017,CARROLL,2 05021,CLAY,0 The last digit after the final "," is a variable value. This is the base file. I have to do further execution on this file later and I need to update the... (7 Replies)
Discussion started by: chagan02
7 Replies

6. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

7. Shell Programming and Scripting

delete last character in all occurences of string

Hello all, I have a file containing the following p1 q1 p2 q2 p1 p2 p3 pr1 pr2 pr1 pr2 pa1 pa2 I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.... (4 Replies)
Discussion started by: bigfoot
4 Replies

8. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

9. Shell Programming and Scripting

how to get the string stored in a variable in a line???

Hi all, I want to search for a data type in a line.For this in a loop i am checking for $DATA_TYPE in a line using grep.But grep is not able to find when i give this. Can any one tell me how to check string in $DATA_TYPE variable in line usign grep (or) any other way to do the above task. ... (4 Replies)
Discussion started by: jisha
4 Replies

10. UNIX for Dummies Questions & Answers

Delete first 2 character from string

Hi ! All, I have to delete first 2 characters from string. How its possible? Like string value is "2001". I required output as "01" Plaese help me. Nitin (4 Replies)
Discussion started by: nitinshinde
4 Replies
Login or Register to Ask a Question