1

I have a for loop that writes text in a file :

for f in $DATA_DIRECTORY 
do 
    echo ' input.'$f '{'
    echo '  copy = ${source.copy}"'$f'.CPY"'
    echo '  data = ${source.data}"'$f'.CSV"'
    echo ' }'
done

But the "f" variable here looks like this :

/path/to/my/file/FILE.TXT

What i want to get is only the name of the file, not the full path and its extension:

FILE

By the way i tried to change my f variable like this so i dont get the extension but it did not work :

{$f%%.*}

1 Answer 1

5

You need two lines; chained operators aren't allowed.

f=${f##*/}  # Strip the directory
f=${f%%.*}  # Strip the extensions

Or, you can use the basename command to strip the directory and one extension (assuming you know what it is) in one line.

f=$(basename "$f" .txt)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.