Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping an exclamation mark in a ssh remote call to awk?

Tags:

ssh

awk

I have this line:

ssh server1 "df -h | sed 's/%/ /g' | awk '{ if (\$5 > 90 && !/^[a-zA-Z]/) { var1=1 }} END { if (var1 == 1) { print 1 } else { print 0 }}'"

However, this produces the following error:

bash: !/^[a-zA-Z]/: event not found

Not quite sure how I should escape the exclamation mark. Any ideas?

Regards,

David

like image 438
davidl Avatar asked Feb 25 '14 15:02

davidl


People also ask

How do I escape exclamation mark in bash?

If you're building a double-quoted string somewhere that isn't echo or printf, like the commit string OP asked about, you can embed $(echo -e "\041") in the string where the exclamation point should be.

How to type exclamation mark in linux?

The exclamation mark is part of history expansion in bash. To use it you need it enclosed in single quotes (eg: 'http://example.org/!132' ). You might try to directly escape it with a backslash ( \ ) before the character (eg: "http://example.org/\!132" ).

What does exclamation mark mean in Linux?

Exclamation mark is for "Not" (like in boolean), -d flag is for folder exists in bash.


1 Answers

Bash evaluates ! history manipulation even in double quotes. And if you escape it within quotes you just get \! (seriously, wtf bash?). You have to end the double quotes, add either '!' or \!, and reopen the double quotes.

ssh server1 "df ... && "\!"/^[a-zA-Z]/...}}'"

For the record, zsh handles the backslash escape within double quotes properly ("\!" -> !).

like image 54
Kevin Avatar answered Nov 15 '22 08:11

Kevin