ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: aniecki on October 21, 2020, 03:13:49 AM

Title: Linux shell environment variable
Post by: aniecki on October 21, 2020, 03:13:49 AM
I am trying to set a PREFIX variable in a bash command line, say
echo $PREFIX
CA_RA

Yet when trying to rename a file, it gives
Tag 'PREFIX' not defined ,
as in the command

exiftool '-testname<$PREFIX-%.4nC.%e'  .

This way would be easier to update bunch of files, not to change the PREFIX on the command.
Thanks!

Title: Re: Lunux shell environment variable
Post by: Phil Harvey on October 21, 2020, 06:38:19 AM
The shell does not interpolate within single quotes.  Use double quotes instead if you want to use shell variables.

- Phil
Title: Re: Linux shell environment variable
Post by: aniecki on October 21, 2020, 12:19:42 PM
exiftool "-testname<$PREFIX-%.4nC.%e"  .

Warning: Invalid tag name 'l-nl_cl-%.4nc.%e'

still, seems not to be working...
Title: Re: Linux shell environment variable
Post by: StarGeek on October 21, 2020, 03:03:12 PM
This is Common Mistake #5c (https://exiftool.org/mistakes.html#M5).  You are not copying any tags, you are setting TestName a static value.  Try this
exiftool "-testname=$PREFIX-%.4nC.%e"  .
Title: Re: Linux shell environment variable
Post by: Phil Harvey on October 21, 2020, 03:24:39 PM
(I should have caught this)
Title: Re: Linux shell environment variable
Post by: aniecki on October 22, 2020, 05:10:17 AM
Is there a way to combine two, eg.

(These do not work, only examples)
exiftool "-testname=$PREFIX-<${Megapixels}%.4nC.%e"  .
exiftool "-testname=$PREFIX" '-testname<${Megapixels}%.4nC.%e'  .


to give outcome:
'./CA_RA-0.110-0001.jpeg'

If this cannot be done in one go, I would run it twice. The second run to skip x-length charters at the beginning of a picture file, if that would be easier.
But perhaps a way to achieve it in one run..

Thanks!
Title: Re: Linux shell environment variable
Post by: Phil Harvey on October 22, 2020, 08:09:41 AM
There are a number of ways to do this.  With bash, escaping "$" in the non-shell variable should work like this:

exiftool "-testname<$PREFIX-\${Megapixels}%.4nC.%e" .

You could also change from double to single quotes:

exiftool "-testname<$PREFIX"'-${Megapixels}%.4nC.%e' .

- Phil
Title: Re: Linux shell environment variable
Post by: aniecki on October 22, 2020, 10:04:43 AM
Thanks guys, indeed both ways work!