user3290180
user3290180

Reputation: 4410

How to store in a variable an echo | cut result?

for example

    echo "filename.pdf" | cut -d'.' -f 1

This way I get the "filename" string. I'd like to store it in a variable called FILE and then use it like this:

    DIR=$PATH/$FILE.txt

So, my script wants to create a file.txt with the same name of the pdf (not a copy of the file, just the name) This way I tried to assign the result of echo | cut

   FILE= 

but I get only "path/.txt" so the filename is missing.

Upvotes: 3

Views: 20980

Answers (3)

chepner
chepner

Reputation: 531055

POSIX parameter expansion would read

file=filename.pdf
filename="${file%%.*}"  # Two % will remove multiple extensions, if applicable
dir=$path/$filename.txt

Upvotes: 2

anubhava
anubhava

Reputation: 785058

So, my script wants to create a file.txt with the same name of the pdf

You can use BASH string manipulation:

s="filename.pdf"
p="${s/%.pdf/.txt}"

echo "$p"
filename.txt

Upvotes: 2

Sean Bright
Sean Bright

Reputation: 120644

FILE=$(echo "filename.pdf" | cut -d'.' -f 1)

Upvotes: 7

Related Questions