{"id":808,"date":"2022-08-30T15:07:31","date_gmt":"2022-08-30T15:07:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/is-it-possible-to-use-variables-in-remote-ssh-command-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:31","modified_gmt":"2022-08-30T15:07:31","slug":"is-it-possible-to-use-variables-in-remote-ssh-command-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/is-it-possible-to-use-variables-in-remote-ssh-command-collection-of-common-programming-errors\/","title":{"rendered":"is it possible to use variables in remote ssh command?-Collection of common programming errors"},"content":{"rendered":"<p>With some inspiration from chepner, I now have a solution that works, but only when called from a bash shell or bash script. It doesn&#8217;t work from tcsh.<\/p>\n<pre><code>ssh my_server \"bash -c 'echo this is \\$HOSTNAME; abc=2; echo abc is \\$abc;'\"\n<\/code><\/pre>\n<p>Based on this, the code below is a local script which runs jabref on a remote server (although with X-forwarding by default and passwordless authentication the user can&#8217;t tell it&#8217;s remote):<\/p>\n<pre><code>#!\/bin\/bash\nif [ -f \"$1\" ]\nthen\n    fname_start=$(echo ${1:0:4})\n    if [ \"$fname_start\" = \"\/tmp\" ]\n    then\n        scp $1 my_server:$1\n        ssh my_server \"bash -c 'source load_module jdk; source load_module jabref; java_exe=\\$(which java); jabref_exe=\\$(which jabref); jabref_dir=\\$(echo \\${jabref_exe%\/bin\/jabref});eval \\$(java -jar \\$jabref_dir\/jabref.jar $1)'\" &amp;\n    else\n        echo input argument must be a file in \/tmp.\nelse\n    echo this function requires 1 argument\nfi\n<\/code><\/pre>\n<p>and this is the 1-line script load_module, since modulecmd sets environment variables and I couldn&#8217;t figure out how to do that without sourcing a script.<\/p>\n<pre><code>eval `\/path\/to\/modulecmd bash load $1`;\n<\/code><\/pre>\n<p>I also looked at heredocs, inspired by how to use ssh to run shell script on a remote machine? and http:\/\/tldp.org\/LDP\/abs\/html\/here-docs.html. The nice part is that it works even from tcsh. I got this working from the command line, but not inside a script. That&#8217;s probably easy enough to fix, but I&#8217;ve got a solution now so I&#8217;m happy \ud83d\ude42<\/p>\n<pre><code>ssh my_server 'bash -s'<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:46:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>With some inspiration from chepner, I now have a solution that works, but only when called from a bash shell or bash script. It doesn&#8217;t work from tcsh. ssh my_server &#8220;bash -c &#8216;echo this is \\$HOSTNAME; abc=2; echo abc is \\$abc;'&#8221; Based on this, the code below is a local script which runs jabref on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-808","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=808"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}