Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing a variable as part of an argument of a command

I am trying to pass a variable as part of an argument to a command. Specifically, I want mysqldump to return elements whose ID is above some number. For example, this line works and returns all elements with ID above 30: mysqldump --user=MYUSER -p -B DB --tables sometable --where='id>30'.

I have a script that determines this number and assigns it to variable LASTID. I would like to pass the value of this variable into the argument (in place of "30"). I run this:

mysqldump --user=MYUSER -p -B DB --tables sometable --where='id>"${LASTID}"'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But it returns the entire database, instead of using the correct id. It is as if it is ignoring what I am passing to LASTID.

I also tried this:

eval $(mysqldump --user=MYUSER -p -B DB --tables sometable --where='id>"${LASTID}"') and eval echo $(mysqldump --user=MYUSER -p -B DB --tables sometable --where='id>"${LASTID}"').

>Solution :

It’s:

mysqldump --user=MYUSER -p -B DB --tables sometable --where="id>${LASTID}"

Research the difference between single and double quotes in shell. Check your scripts with shellcheck. Eval is one letter from evil – do not use eval.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading