I have a line like this. It breaks with an error, no such file or directory.
LB=$(ls "$f *.backup" | sort --reverse | head -n 1)
If I do this, it kinda works, just spits wrong filenames:
LB=$(ls $f *.backup | sort --reverse | head -n 1)
How do I solve this?
Filename expansion wildcards aren't evaluated in quotes. You'd have to escape the space or include it in quotes (And leave the wildcard out). And you don't want to parse ls
. One way to safely get the last file based on alphabetical order, no matter the filenames and if they have newlines or other funny characters in them:
files=( "$f "*.backup )
lb="${files[-1]}"
That is, use an array to hold all the expanded files (Sorted by name) and then get the last element. You also shouldn't use upper case variable names unless you're exporting it to the environment of child processes.