if [ "$VAR1"| cut -c 7-18 = "GHIJKLMN.csv" ]; then
What this does is execute [ "$VAR1"
and pipe the output to cut -c 7-18 = "GHIJKLMN.csv" ]
. [
will fail because it requires its last argument to be ]
, and cut
will fail because probably neither =
nor ]
are files.
I'm going to guess what you wanted was:
if [ "$(echo "$VAR1" | cut -c 7-18)" = "GHIJKLMN.csv" ]; then
Notice how I wrap the command in "$()"
. That causes it to execute and substitute its place with the output. Quotes are important, so the output is not split into multiple arguments for [
.
Following what I believe to be best practices for bash, I would recommend doing this, instead:
if [[ "$(cut -c 7-18 <<< "$VAR1")" = "GHIJKLMN.csv" ]]; then
This avoids special interpretation of $VAR1
by echo (depending on content, it might mistake it for options), and calling 2 needless executables, [
and echo
. Something special to note here is that you don't really need any of the quotes I used. [[
is a special syntax of bash, and even if the substitution had spaces, bash would not word-split it. It would also not split what was given to <<<
. GHIJKLMN.csv
does not have any whitespace or special characters, so it would also be fine. When in doubt though, it's a good habit to quote everything.
You could've also skipped the call to cut
by doing this:
if [[ "${VAR1:6:12}" = "GHIJKLMN.csv" ]]; then
Now, everything is done in bash without executing any other program. In this syntax, instead of giving the range of characters you want like you did to cut
, you provide the 0-based offset to the first character and the number of characters you want. That's 7 - 1 because cut
's indices start with 1, and 12 because the range 7-18 is inclusive, so 18 - 7 + 1.
I just saw, your $VAR1
is "ABCDEFGHIJKLMN.csv"
. Since you cut to the end, you don't really need to specify the count:
if [[ "${VAR1:6}" = "GHIJKLMN.csv" ]]; then
"$VAR1"| cut -c 7-18
works as you describe - did you meanecho "$VAR1"| cut -c 7-18
perhaps?