Explaining Different Approaches to Line Continuations in Bash, Python and Powershell
The aim of this page📝 is to review the concept of line discontinuation in bash, python and powershell.
2 min readFeb 3, 2023
The escape \
provides a means of writing a multi-line command.
- By default — each separate line constitutes a different command in Bash
- Escape character —
\
- withinecho
command at the end of line escapes the newline, provided you put it into double quotes (quoting is a different story altogether)
echo "This will print
as two lines."
>>> This will print
>>> as two lines.
echo "This will print \
as one line"
>>> This will print as one line.
- What matters here is escape character outside of strings, at the end of script lines
- If a script line ends with a pipe
|
then an escape\
is unnecessary as pipe is a "natural line continuator" - According Advanced Bash-Scripting Guide, it is a good programming practice to always escape the EOL of code continuing into the newline
- I have not been doing this in python, nor in powershell
- Quite the opposite!
- In Powershell, my approach has been defined by Get-PowerShellBlog > Bye Bye Backtick: Natural Line Continuations in PowerShell and I would never use a
`
(backtick, which is an escape char) as it's just invisible - In Python, my approach has been, naturally, defined by https://pep8.org/#maximum-line-length which clearly prefers implied line continuation inside parentheses, brackets and braces
- Let’s stay in bash and conclude that the following are synonymous and the third one is recommended if you want to break code into multiple lines
echo "$(pwd)" | cut -d "/" -f5
>>> Admin
echo "$(pwd)" |
cut -d "/" -f5
>>> Admin
echo "$(pwd)" | \
cut -d "/" -f 5
>>> Admin