1

I want to trim this string /tmp/files/ from a variable $FILES For example:

setenv FILES=/tmp/files/list
ONLY_L=`trim($FILES,'/tmp/files/')`
echo $ONLY_L
#should see only 'list'

I though of using sed for the job, but it look a little "ugly" because all the \ that came before the /.

1
  • 1
    setenv is a csh command, not a bash command. Commented Feb 24, 2014 at 15:35

4 Answers 4

3

For sed, you don't have to use /

For instance, this works as well:

echo $FILES | sed 's#/tmp/files/##'
Sign up to request clarification or add additional context in comments.

Comments

1

ONLY_L="${FILES##*/}"

or

ONLY_L="$(basename "$FILES")"

or

ONLY_L="$(echo "$FILES" | sed 's|.*/||')"

does what you want

Comments

1

You should use the basename command for this. It automatically removes the path and leaves just the filename:

basename /tmp/files/list

Output:

list

Comments

0

You don't need sed or call tools. bash provides you this ability using string substitution.

$ FILES='/tmp/files/list'

# do this
$ echo "${FILES/\/tmp\/files\/}"
list

# or this
$ echo "${FILES##*/}"
list 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.