Skip to content
Advertisement

Bash script runs one command before previous. I want them one after the other

So part of my script is as follows:

JavaScript

In my output I see:

JavaScript

Whereas when I ssh to the server, xyz.log does exist within ~/a/b/c/

Why is the grep statement getting executed before the echo statement?

Can someone please help?

Advertisement

Answer

The problem here is that your command in backticks is being run locally, not on the remote end of the SSH connection. Thus, it runs before you’ve even connected to the remote system at all! (This is true for all expansions that run in double-quotes, so the $foo in echo $foo as well).

Use a quoted heredoc to protect your code against local evaluation:

JavaScript

If you want to pass through a variable from the local side, the easy way is with positional parameters:

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