I want to do the following on a bash script:

I want the user to enter a file name and the bash script must move the file to another file as follows. If the user enters input.tex, I want to move this file to another file named inputmerged.tex.

I have the following bash script named myscript.sh:

mv $1 $2

The above however needs to be run as ./myscript input.tex inputmerged.tex The second input is redundant, since the new file, which I move to, is of the same type and always has the word merged appended at the end of the first filename. How should I do it to get rid of the second input in the bash script?


1 Answer 1


Something like:

NEWFILE=`echo ${1} | sed "s/\./merged./"`
mv "${1}" "${NEWFILE}"

This will however fail if there are multiple . characters in the name.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy