Reputation:
I'm trying to write a Python program to a file in one line of Bash. I am trying this:
$ printf "#\!/usr/bin/python3\nprint('hi\!')\nwhatever()" > whatever.py
But that gives:
#\!/usr/bin/python3
print('Hi\!')
whatever()
How can I get it to output:
#!/usr/bin/python3
print('Hi!')
whatever()
Upvotes: 1
Views: 54
Reputation: 27215
The best way would be to use a Here Document:
cat > whatever.py <<EndOfPythonProgram
#!/usr/bin/python3
print('hi!')
whatever()
EndOfPythonProgram
But since you wanted to write everything in one line, you may use the following command:
printf '%s\n' '#!/usr/bin/python3' "print('hi"'!'"')" 'whatever()' > whatever.py
printf %s yourString
instead of printf yourString
. That's the way printf
is supposed to be used (not necessary in this case, but safer for general cases). %s\n
every additional argument of printf
is printed as one line.!
.!
in single quotes and the remaining parts in double quotes. In bash
the string ab
can be written as: ab
, "ab"
, 'ab'
, "a"'b'
, ...Upvotes: 2
Reputation: 785128
You can use:
set +H # disable history expansion
printf "#!/usr/bin/python3\nprint('hi!')\nwhatever()\n"
set -H # enable history expansion again
#!/usr/bin/python3
print('hi!')
whatever()
Upvotes: 0