Bracing against the wind  
www.documentroot.com  

Thursday, May 08, 2008

Useful Line for Quoting Shell Arguments

Safely pass "@*" to ssh without having to worry about the remote shell re-expanding variables.

qa=`perl -e 'for (@ARGV) {s/(["\\$])/\\\\$1/g; $_="\"$_\"";} print join(" ", @ARGV)' "$@"`
ssh remote-server "cd /some/where; do something; $qa"

Single quote version is safer, but is harder for me to read.

qa=`perl -e "for (@ARGV) {s/'/'\\\\\''/g; \\\$_=\"'\\\$_'\";} print join(' ', @ARGV);" "$@"`

Labels: ,


[View/Post Comments] [Digg] [Del.icio.us] [Stumble]

Home | Email me when this weblog updates: | View Archive

(C) 2002 Erik Aronesty/DocumentRoot.Com. Right to copy, without attribution, is given freely to anyone for any reason.


Listed on BlogShares | Bloghop: the best pretty good | Blogarama | Technorati | Blogwise