How to use bash $(awk) in single ssh-command?

bash awk
ssh awk escape quote
use awk in ssh
ssh remote command
awk: cmd. line:1: ^ unexpected newline or end of string
ssh script
shell script to login to multiple servers and execute commands

I am trying to execute a single command over ssh that contains `sub-code` or $(sub-code) (I use it all the time but I don't know the official name for that) to be executed first, but on the target server.

For the sake of argument, let's say this is the command I want to use. Ofcourse this can be done with hostname, but this is just a simplified example that contains all the formatting wizardry I want to use.

echo `uname -a | awk '{print $2}'`

No problem. But how do you escape this properly to send it over ssh in a single command? The following is wrong, because it lets the server reply your local hostname. The sub-code is executed locally:

ssh myServer echo `uname -a | awk '{print $2}'`

But any escape-ishness that comes to mind doesn't work:

$ ssh myServer echo \`uname -a | awk '{print $2}'\`
awk: cmd. line:1: {print $2}`
awk: cmd. line:1:           ^ invalid char '`' in expression
$ ssh myServer echo \$(uname -a | awk '{print $2}')
bash: syntax error near unexpected token `('
$ ssh myServer echo \$\(uname -a | awk '{print $2}')
bash: syntax error near unexpected token `)'
$ ssh myServer echo \$\(uname -a | awk '{print $2}'\)
awk: cmd. line:1: {print $2})
awk: cmd. line:1:           ^ syntax error
bash: -c: line 0: unexpected EOF while looking for matching `)'
bash: -c: line 1: syntax error: unexpected end of file

I would like an answer that includes using properly escaped ` or $() because I'd like to know if it's possible.echo` could be something more complicated.

Try this

ssh myServer "uname -a | awk '{print \$2}' "

Use the double quotes " in order to group the commands you will execute remotely.

You also need to escape the $ in the $2 argument, so that it is not calculated locally, but passed on to awk remotely.

Edit:

If you want to include the $( ), you again have to escape the $ symbol, like this:

ssh myServer "echo \$(uname -a | awk '{print \$2}') "

You can also escape the backtick `, like this:

ssh myServer "echo \`uname -a | awk '{print \$2}'\` "

How to print a specific column with awk on a remote ssh session , You're hitting a quoting problem; the $5 is being interpreted at the wrong time. There are at least two solutions: Put a \ before the $ ; e.g.. /usr/bin/ssh -i  Tell the shell which executable to use to run the script. Prepare awk to use the FS field separator variable to read input text with fields separated by colons (:). Use the OFS output field separator to tell awk to use colons (:) to separate fields in the output. Set a counter to 0 (zero).

What about piping the output of the command and run awk locally?

ssh yourhost uname -a | awk '{ print $2 } '

Executing awk in a remote server using ssh, I'm trying to execute a simple awk command as follows, echo 1 2 | awk '{print \\$2}​' When i try to execute the same command in a remote server using ssh as follows, The single quotes do not prevent variable expansion in a here document; 2>/dev/null if then echo "$1 is file" for server in $(cat $1) do # echo $​server . Run a remote awk command using ssh. Browse other questions tagged bash shell ssh awk or ask your own question. Why does awk fail when part of ssh command? 1.

The workaround is to create an sh file with the command then pass it to ssh command:

test.sh

echo `uname -a | awk '{print $2}'`

and command:

ssh myServer 'bash -s' < test.sh

UPDATE:

The command without quotes works fine as well:

ssh myServer uname -a | awk '{print $2}'

Bash Command Awk Via Ssh, Something is not right with the awk command. I tried putting for i in $(cat db_hostlist2.txt | grep -v '^#'); do echo ""Checking for $i""; ssh admin@$i '. USE OF THIS SYSTEM IS EXPRESSED CONSENT Single quotes do I found a partial solution to my problem. Just to let you know, in this case $2 was being treated as a shell variable and not as awk variable This works and gives me the output 2 ssh user@host <<HERE > echo 1 2 | awk '{print \$2}' > HERE But i'm still not able to figure out why it works when a command file is given as input to ssh without an escape sequence

It is better to use a heredoc with ssh to avoid escaping quotes everywhere in a more complex command:

ssh -T myServer <<-'EOF'
uname -a | awk '{print $2}'
exit
EOF

How To Use awk In Bash Scripting, Send output to sort command using a shell pipe: awk The part outside the curly braces is called the “pattern”, and the part inside is the “action”. -p $BASE || : FILES="$(ldd $pFILE | awk '{ print $3 }' |egrep -v ^'\(')" for i in $FILES do dcc="$(​dirname $i)" [ ! Top 20 OpenSSH Server Best Security Practices. This is not ssh or awk handling the quotes, it is the shell (and they are necessary to keep the shell from handling other characters specially, like $).Nesting them is not supported (although other structures, such as $() may nest even while containing quotes), so you'll need to escape the single quotes separately.

If you are trying to get the hostname, use:

ssh myServer "uname -n"

escaping awk with remote ssh command and bash that is already , You can't backslash-escape single quotes inside single-quoted strings, but you can backslash-escape double quotes inside a double-quoted  Awk is a programming language and utility included in all Linux distributions that can be used to manipulate and process text files in a fine-grained manner. In this guide, we will discuss the basics of how to use awk to process text files efficiently

Learning Linux Commands: awk, You can write awk scripts for complex operations or you can use awk from the command line. You will need some command-line knowledge in Linux and possibly some Just as sed, awk reads one line at a time, performs some action LTS Focal Fossa · Enable SSH root login on Debian Linux Server  In order to filter text, one has to use a text filtering tool such as awk. You can think of awk as a programming language of its own. But for the scope of this guide to using awk, we shall cover it as a simple command line filtering tool. The general syntax of awk is: # awk 'script' filename. Where 'script' is a set of commands that are

bash, ssh -p 2222 "$p" 'who -b' < /dev/null | awk '{print $(NF-1)" "$NF}' >> 'file2' answer - is the canonical way to read one line of input with the read builtin. The key is Use the default values of the input (hosts.txt) and the output  This is the kind of situation where I would highly recommend using single quotes instead of a here document. Even if you have some variables you want to insert, you're better off using a single-quoted string and printf to expand variables -- that's still going to be easier to manage than trying to escape all the shell variables throughout.

101 Bash Commands and Tips for Beginners to Experts, but Unix and Unix-like systems only have a single root directory called \ . Occasionally, we'll want to run one command right after another. To do Causes scp and ssh(1) to print debugging messages about their progress. prennix$ set -x; for number in $(seq 1 3); \ do echo "Number ${number}"; done;  awk is a splendid Unix scripting language for processing text files. The version included in most Linux distros is GNU awk, or gawk for short. I like it for pulling data from ordered data sets, such as text lists and CSV exports from spreadsheets. awk sees each line in a file as a separate record, …

Comments
  • why echo `command`? why not command?
  • @anishsane because he wants to see how to pass a backtick or a $( through ssh to a remote server
  • @Redsandro I think I figured out how to escape the $( and the backtick on ssh. Just updated my answer...
  • If you execute in sub-command like res=`ls | awk '{print \\$2}'`, we need two escape here.
  • Yeah I was trying sshpass to execute /proc/loadavg on remote server and using awk to filter the result. Thanks for the answer. The escape sequence did the trick!. Thanks
  • Thanks, this works in certain cases. However, for more versatility, user000001's answer does exactly what I want. :)
  • @"The command without quotes works fine as well:" the awk being executed is local awk, not on the remote system.
  • Yes, commands from all 3 questions run the right part (after the pipe) locally. So the only solution is to save the command in the script.
  • Thanks, this works. Upvoted. But user000001 does exactly what I want in a single command, I've accepted that one. :)
  • "So the only solution is to save the command in the script" : I object the "only" word. probably you meant "easiest".
  • sorry, I mean answers. I've tested the last command of user000001 and it works on the remote server so I was wrong.