I’m trying to get my script to ls all the files in the form “$variable”.*
the parameter is always a file with the extension .001
I want to find all the files with the same name but different extension, eg $file.002, file.003 etc. eg.:
first="$1"
others=${first%.001}
ls $others"*"
My problem is the file is named file[success].mpg.001
and what gets fed to ls is file[success].mpg* which gives me
ls: cannot access file[success].mpg*: No such file or directory
because ls needs to see:
file\[success\].mpg*
I’ve been trying everything, the one thing I did notice is
ls fed the parameter $1 works, but not if fed $file after I’ve done file=$1 .
So I guess how do I change the variable into a parameter?
You could use
bash‘sprintfcommand to reformat the string. Furthermore you don’t need to quote*:printf %qreformats the string in a format that can be used as shell input (regarding tobash‘s man page).edit regarding to a comment:
The above solution does not work with white spaces in file names. For those cases (as some other answers already mentioned) it’s better not to use
printfbut to properly quote all variables: