0

I log in through ssh and run bash. In these bash commands, I need to call a specific function in a python file and pass a string argument into this function.

What I have right now:

echo "commands" | ssh into instance

where commands contains

python -c 'from python file import myfunction;
            myfunction({}) ' ... .format(myvariable)

myvariable is a string containing multiple words.

However, when ssh completes and the commands are executed, debugging logs show that the quotes around myvariable are gone and when passed into myfunction, I get an error for undefined input variables.

How do I make sure myvariable stays a string?

1
  • Not an answer to your question, but you may want to look into fabric instead of printing commands Commented Jul 2, 2014 at 19:11

1 Answer 1

1

This is usually caused by thinking quotes are context aware. They're not.

Example:

var="select * from table where name="value" order by name"

As a human you might think that since this is a SQL statement, obviously bash will know that the quotes in the middle are nested, and will read the string like this:

var="select * from table where name="value" order by name"
                                    |-----| Nested
    |----------------------------------------------------| Everything

Bash, however, isn't that smart. It reads the string like this:

var="select * from table where name="value" order by name"
    |-------------------------------|     |--------------|
           Quoted string #1                   Quoted #2

The literal values are concatenated, with the net effect that the middle quotes "disappear". The statement is equivalent to:

var="select * from table where name=value order by name"

Instead, you have to tell bash that the quotes are literal data with escapes:

var="select * from table where name=\"value\" order by name"

Shellcheck automatically warns about this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.