-1

While executing the below script:

export var1="string('\n')"

echo $var1

I get the output as string(n). But I expect to see the output as : "string('\n')".

Is there any way to get that?

Note: I need the output to be displayed in string('\n') (\n in single quotes) to use that variable in m_wc abinitio command.

Update: Thanks for responding quickly, but the variable text goes to next line when '\n' occurs. output below:

export var='string('\''\n'\'')'
export var1="string('\\n')"
echo "$var1"
string('
')
>echo $var
string('
')

Is there any way to escape the '\n' entirely?

Update : printf '%s\n' "$var1" seems to be working fine, but how can i put this value to a variable and use it anywhere in the script ?

I tried to put like below : but does not work:

export var1="string('\n')"

export var2="printf '%s\n' "$var1""

export BYT_CNT=m_wc -no-commas -string $var2 /prod/users/edw/gvx770/list.dat|awk '{print $1}'

echo $var2

echo $var2 printf '%s ' string(' ')

sanju
  • 7
  • 2
  • 1
    Don't use `echo`; use `printf '%s\n' "$var1"`. You don't know what different versions of `echo` are going to do; different shells handle backslash escapes in arguments differently. `printf` is consistent and standardized by POSIX — and does what you want. Note the single quotes around the format string `'%s\n'` and the double quotes around `"$var1"`; both are a good idea, for different reasons. – Jonathan Leffler Sep 01 '14 at 23:41

3 Answers3

0

The easiest thing to do is probably:

export var1="string('\\n')"
echo "$var1"
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • ...also checkout this brief article on **[escape sequences in UNIX shell](http://steve-parker.org/sh/escape.shtml)**. – TheCodeArtist Aug 30 '14 at 05:25
  • Hi William, Thanks for responding but it variable text goes to next line when '\n' occurs. output below: export var1="string('\\n')" >echo "$var1" string(' ') Is there any way to escape \n ? – sanju Sep 01 '14 at 23:08
0

This will do what you want:

export var='string('\''\n'\'')'

What you need to know is:

  1. You can include anything between single-quotes except for an apostrophe; and
  2. You can quote an apostrophe by preceding it with a backslash, outside of any single- or double-quoting.

With those two rules, you can quote anything in shell.

Alternatively, you can use double-quotes, but the rules for using it effectively are more complex.

aecolley
  • 1,973
  • 11
  • 10
0

Finally, got the solution to fix this issue. Here is the code:

export dir="string('\n')"

export j=$(printf "%s\n" $dir)

export BYT_CNT=m_wc -no-commas -string "$j" /temp/users/list.dat | awk '{print $1}'

echo $BYT_CNT

Thanks guys for all your help.

sanju
  • 7
  • 2