My shell is dash. My problem is the following:# A=abc\nde fg# printf $Aabcde fg## B=abc\\nde fg# printf $Babcde fg## C=abc\\\nde fg# printf $Cabc\nde fg# The string variable I'm working with has both spaces and \n and possibly other whitespace characters. I would like to access the variable $A without escaping the \n in the string. Using single quotes would not interpret it as a variable and using double quotes interprets those special characters.Does dash have a way built in to the shell to do so? I know I can send the variable to an external program that will double the backslashes for the escape characters in the string, but that just feels wrong.EDIT:I realize my mistake with printf but it's unrelated in this case. Initially I was using echo. This is a better example of what I'm asking. Here are two of the exact same command sequences, only the first is in bash and the second is in dash:bash$ cat sample.txtabc\nde fgsome string$ A=`cat sample.txt`$ echo $Aabc\nde fgsome string$ dash$ cat sample.txtabc\nde fgsome string$ A=`cat sample.txt`$ echo $Aabcde fgsome string$ I would like to know if I can have dash not interpret the \n in this sequence of commands. I have already implemented a workaround using an external command. The proper use of printf does what I want, but I'm curious to see if the dash shell itself has some way of doing this its own.Here's the reason the question came up, for completeness. I need to read a file and CONDITIONALLY comment or uncomment the bottom part. The first part has a literal \n (not newline) that needs to stay that way in the rewritten file.I split the file into 2 pieces using sed enclosed in backticks and assign those to variables. At the end I concatenate the variables by simply doing something like $VAR1$VAR2, where $VAR2 has already been conditionally commented, uncommented, or left alone.
Print variable with backslashes in dash
shell;echo;printf;dash
Your problem is that Bash and Dash both have echo as a builtin, but the two implementations do not behave the same when given a string containing \:echo 'abc\ndef'This produces different results on the two shells.You can get consistent output by using printf '%s\n'; although this is also a builtin, it produces the same output on both shells (this is not true of all conversions, e.g. bash provides a %q conversion which dash does not):printf '%s\n' 'abc\ndef'This produces abc\ndef in both shells.You could alternatively use an external implementation of echo; on Linux, coreutils gives you GNU echo in /bin/echo:/bin/echo 'abc\ndef'Note, however, that this is less portable.  The GNU info file has this to say:POSIX does not require support for any options, and says that the  behavior of echo is implementation-defined if any string contains a  backslash or if the first argument is -n.  Portable programs can use  the printf command if they need to omit trailing newlines or output  control characters or backslashes.