I have a directory that looks like so :
/path/to/files/data-file.1
/path/to/files/data-file.2
/path/to/files/data-file.3
/path/to/files/data-file.4
/path/to/files/data-file.5
/path/to/files/data-file.6
I'd like to make a script to list all the files between two values in variables like the following :
#!/bin/bash
fileA="/path/to/files/data-file.2"
fileB="/path/to/files/data-file.5"
ls -v /path/to/files/*-file.[0-9]* | sed -n '/$fileA/,/$fileB/p'
with the output :
/path/to/files/data-file.2
/path/to/files/data-file.3
/path/to/files/data-file.4
/path/to/files/data-file.5
However, the sed command does not seem to work. I'm guessing because of the slashes in the variables. Is there a way to overcome this without changing the variables ? I'd like to use this bit of code in a script where fileA and fileB are dynamic.
You can use a different delimiter if you escape it at its first occurrence, so the slashes will not confuse
sed
. However, note that using single quotes will result inbash
not substituting variables, so in your example,sed
looks for$fileA
and$fileB
literally, as the variables does not get expanded. You need to use double quotes if you use variables.So, something like this should work: