Delete first character from a string stored in a variable

Hallo!

Example.

#!/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

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!

Yes:

${BACKUP_DIR#?}

Try using SED

echo $BACKUP_DIR |sed 's/^.//'
DIR=${DIR:1:${#DIR}}

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...

@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.

BIG THANKS, TO ALL FOR HELP!

Lets say "radoulov" 10 points! OK! :slight_smile:

Another one ?

${BACKUP_DIR:1}

Good, but no POSIX :wink:

sh: Bad substitution
echo ${BACKUP_DIR/\//}

This is good also but no POSIX :o

Trying to understand how things worked.
I found myself a solution (if you know the first character is the slash).

lucid@gnomelynx:/$ BACKUP_DIR=/home/userx/backups/evolution
lucid@gnomelynx:/$ echo $BACKUP_DIR
/home/userx/backups/evolution
lucid@gnomelynx:/$ echo ${BACKUP_DIR#*/}
home/userx/backups/evolution
lucid@gnomelynx:/$ 

Hi, linuxinho,

In this case - since the first character is a / - this command is equivalent to:

echo ${BACKUP_DIR#/}

Which in this case is has the same effect as:

echo ${BACKUP_DIR#?}

which was given by radoulov and that deletes any first character