Skip to content
Advertisement

Nested grep with SSH

I have a following Shell command which runs successfully:

JavaScript

However, I have to SSH to a particular machine to run this. To avoid this, I modified it in following way which leads to the failure:

JavaScript

What is wrong with this command? How can I fix this?

Advertisement

Answer

Embed your code in a quoted heredoc to avoid needing to modify it (which is an error-prone process by nature):

JavaScript

In the specific case here, the obvious reason that your original modifications failed was internal single quotes terminating the ones surrounding the code as a whole.

Specifically:

JavaScript

…the first ' in that expression terminates the one opening before the command, so the space isn’t syntactically protected.

You could instead do the following, since your shell is bash:

JavaScript

$'' is an extension within which backslashes can escape single quotes (and also be used for other literals — n for a newline, t for a tab, etc), functionality not available with typical POSIX sh syntax.

By contrast, in POSIX sh, one can switch quoting types to embed a literal single-quote: 'foo'"'"'bar' to defines a string with both foo and bar single-quoted, and separated by a single literal single-quote between them — perfectly valid syntax, but not necessarily easy for readers unfamiliar with the idiom to process).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement