4

The program takes single character as an argument.

./myprog <character>

How can i pass \n in the shell to the myprog ?

Nouman Tajik
  • 433
  • 1
  • 5
  • 18

1 Answers1

11

Easiest but not very pretty:

./myprog "
"

EDIT: Still easiest, but I forgot about it:

./myprog $'\n'

Another alternative:

./myprog ^M                  # not ^ and M, but the literal LF character
                             # in `bash`, obtained by Ctrl-V, Enter

Or, you can do what these programs do, and parse the argument yourself. If you say ./myprog "\n", your code will receive the two-character string \n as its first argument - look for such a sequence and translate it to a newline (e.g. by passing the argument through printf or equivalent in your language, or by regular expression substitution...).

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • ./myprog $(echo -e "\n") ./myprog $(printf "\n") These two doesn't work in my bash but ./myprog " " and ./myprog $'\n' works. thanks – Nouman Tajik Nov 08 '18 at 04:59
  • 1
    @Amadan The ones with `$( )` won't work even with quotes, because that strips trailing newlines from the output it captures. Backticks won't work either, because they do the same thing. – Gordon Davisson Nov 08 '18 at 07:04