dashman
dashman

Reputation: 3028

Extract the filename from a script variable

I've got a batch file like

for f in bin/$1*.txt; do echo $f; done

I'd like to echo just the filename - not bin/temp.txt or temp.txt - just temp.

How I can extract the filename from $f?

Upvotes: 0

Views: 62

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15418

Parameter expansion.

$: f=bin/temp123.txt # an example you might have
$: x="${f#bin/}"     # asign to x without bin/ prefix
$: echo $x
temp123.txt
$: echo ${x%.txt}    # show x without .txt suffix
temp123

Upvotes: 0

user10990153
user10990153

Reputation:

If the suffix is always the same you can just use basename -s ".txt" $f.

Upvotes: 1

Related Questions