How to escape special characters in a Bash string in Linux?

The problem is with ssh that makes a quoted string to more than one if there are spaces. For example,

ssh user@host cmd "my string"

The cmd on host will be executed like

cmd my string

rather than

cmd "my string"

It will only work if the string is escaped like

ssh user@host cmd my string

Space is one example, there are many more other special characters in Bash.

The question is how to escape special characters in a Bash string in Linux nicely and easily?

The printf command line tool from GNU coreutils has an interpreted sequence %q that does escaping.

%q
  ARGUMENT is printed in a format that can be reused as shell input, 
  escaping non-printable characters with the proposed POSIX $'' syntax.

So, after escaping, the string can be well passed to bash -c for execution. An example is as follows.

$ function ac { echo $#; }; ac "hello world" new world
3

$ printf '%q' 'function ac { echo $#; }; ac "hello world" new world'
function\ ac\ \{\ echo\ \$#\;\ \}\;\ ac\ \"hello\ world\"\ new\ world

$ bash -c function\ ac\ \{\ echo\ \$#\;\ \}\;\ ac\ \"hello\ world\"\ new\ world
3

It can also be passed through ssh to execute on a remote host:

$ ssh localhost 'bash -c function\ ac\ \{\ echo\ \$#\;\ \}\;\ ac\ \"hello\ world\"\ new\ world'
3

Similar Posts

  • Most important aspects or features of the C++ programming language?

    Could you list the most important features or aspects for the C++ programming languages that describe its most important features/domains that are different from the others? For C++, as in Going Native 2012 Keynote by Bjarne Stroustrup: light-weight abstraction Key strengths: infrastructure software resource-constrained applications Read more: How to make WordPress regenerate the thumbnails after…

  • |

    Vim + cgdb

    I begin to use vim for programming. I like the tools that focus on one function. I used to use emacs. But I think I like vim more. For debugging, I use gdb. But I use the front end cgdb. I can see the codes with the cursor when debugging. I can use F8 for…

  • Bash comparison operators

    What are all the bash supported comparison operators? The man 1 test contains all the operators supported in bash. An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of: ( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1…

  • Java与C++在语言方面上的不同

    1,Java抛弃了头文件、预处理机制、goto语句和多维数组。 2,Java不支持指针。 3,Java抛弃了联合体和结构体。 4,Java不支持独立函数。所有函数必须在类中声明。 5,Java不支持多重继承,可以使用接口模拟多重继承。 6,Java不支持运算符重载。 7,Java中布尔型不再用整数来代替。 8,Java中主函数必须有一个字符串类型的参数。 Java抛弃的C++中的这些机制和结构多数都是“危险”的,减少了语言的复杂性,增强了安全性,虽然在一定程度上减少了其灵活性。 Read more: Java Calling Native Functions in .DLL on Windows OCaml Learning Materials Vim Indenting C/C++ Code COM Port Programming in Win32 C++ Reference and Styles Profiling Vim to Find Out Which Plugin Makes Vim Slow How to remove newline characters from a string in C++?…

  • Chrome does not work with ibus

    Seems the input method on Linux (ibus) does not work well with Chrome. But other programs, e.g. thunderbird, has not problem with ibus at all. What’s wrong with Chrome or ibus? Check whether the “Qt IBus library and Qt input method plugin” is installed. If not, installing it and restarting ibus may help: # yum…