shell - How to separate multiple commands passed to eval in bash -


i'm trying evaluate multiple lines of shell commands using eval, when try resolve variables eval separated newline \n variables not resolved.

x='echo a' y='echo b' z="$x\n$y" eval $x eval $y eval $z 

which outputs:

a b anecho b 

the last command gives anecho b, , apparently \n treated n there. there way evaluate multiple lines of commands (say, separated \n)?

\n not newline; it's escape sequence in situations translated newline, haven't used in 1 of situations. variable $z doesn't wind containing newline, backslash followed "n". result, what's being executed:

$ echo a\necho b anecho b 

you can either use semicolon instead (which requires no translation), or use \n in context will translated newline:

$ newline=$'\n' $ x='echo a' $ y='echo b' $ z="$x$newline$y" $ eval "$z" b 

note double-quotes around "$z" -- they're critical here. without them, bash word-split value of $z, turning whitespace (spaces, tabs, newlines) word breaks. if happens, eval receive words "echo" "a" "echo" b", turning newline space:

$ eval $z echo b 

this yet in long list of cases it's important double-quote variable references.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -