Menu

Using a variable from the shell in the if option

2026-08-17
2026-08-17
  • Angel_Blue01

    Angel_Blue01 - 2026-08-17

    exiftool version: 13.25 (Linux, Debian 13)

    I am trying to use a variable to set a value in a shell script in the if option.

    exiftool -if '$iptc:keywords =~ /animals/i' -filename "$2"

    This works as expected, returning the filename passed as the second argument from the shell if the keywords property contains a value of animals, as per the exiftool documentation.

    However, I just cannot find a way to use a variable instead of hardcoding the value. I first tried just using the variable name:

    exiftool -if '$iptc:keywords =~ /$1/i' -filename "$2"

    but this fails. I expect that this would be because the variable $1 is not being expanded.

    I tried using double quotes, which I would expect would be expanded in my shell, bash:

    exiftool -if "$iptc:keywords =~ /$1/i" -filename "$2"

    This fails.

    How can I use a variable in an if?

     

    Last edit: Angel_Blue01 2026-08-17
  • StarGeek

    StarGeek - 2026-08-17

    This is more of a Linux question about the shell you are using rather than an exiftool one. So my knowledge is limited.

    But I believe you have to have double quotes around your first variable and it has to be outside of the single quotes. I think it would be something like this
    exiftool -if '$iptc:keywords =~ /'"$1"'/i' -filename "$2"

     
  • Angel_Blue01

    Angel_Blue01 - 2026-08-17

    That works, thank you!

     

Log in to post a comment.