I am writing a java program from terminal using printf
and redirecting its output to a .java
file but printf
fails to interpret the horizontal backslash tab (\t
), and when I have an exclamation mark (!) in the string, it doesn't even print and fails with this error:
bash: !": event not found
How do I force printf to include a horizontal tab?
And how do I include an exclamation without getting the above error?
commandline argument:
$ printf "%s\n" "public class {" "\tpublic static void main(String[] args) {" "dogBark()" "}" "public static void dogBark() {" "System.out.println("Woof")" "}" "}" > barkingDog.java
output from .java
file
$ less barkingDog.java
public class {
\tpublic static void main(String[] args) {
dogBark()
}
public static void dogBark() {
System.out.println(Woof)
}
}
Woof!
and double for everything else but the error still persists. Does it have to be the other way around? – bit Jul 07 '18 at 20:39'Woof!'
is wrapped within double quotes, which will escape the single quotes. So yes, try the other way around. – jesse_b Jul 07 '18 at 20:42s
after%
to ab
causedprintf
to start interpreting the horizontal tab\t
when previously it wasn't? – bit Jul 09 '18 at 20:33