- How to Use a Bash Multiline Command
- Long command lines and a bash multiline command
- Using a backslash \ or « to print a bash multiline command
- Write a comment with the hash mark (#)
- The bash multiline command with “()”
- Use Linux Reader to open Linux files
- Related articles
- Split Long Bash Command into Multiple Lines in a Script
- How to Use Bash Backslash To Split Long Commands
- Conclusion
- About the author
- John Otieno
- How to enter a multi-line command in bash on command line
- 3 Answers 3
How to Use a Bash Multiline Command
Bash multiline commands simply refer to commands that span more than one line. In this article, you will understand more on how bash reads/handles multiline commands. Also, at the end of this article, a handy software utility is introduced. This software utility will help you to read and access Linux files on a Windows PC without any stress.
- when you can use a bash multiline command
- how to print a multiline command
- when DiskInternals can help you
Long command lines and a bash multiline command
When your script contains long command lines, bash will continue to read the commands non-stop; if the cursor gets to the right-hand end, bash continues to the next line automatically. However, if you want bash to read your script line by line, then you must introduce a backslash (\) at the end of each line. Also, bash doesn’t support multiline comments; you have to write the comments serially.
Using a backslash \ or « to print a bash multiline command
If you’re writing a multiline command for bash, then you must add a backslash (\) at the end of each line. For multiline comments, you have to use the HereDoc « tag.
Multiline comments written serially:
Multiline comments written with «:
Bash will ignore everything in here.
Note: you must repeat the command you used in the first line at the end of the comment, and it should be written in ALL CAPS.
Practical Example:
Write a comment with the hash mark (#)
Using # is the simplest way to write comments in bash. It is the most common way of writing comments.
The bash multiline command with “()”
Some users have confirmed that using brackets “()” can help to tell bash that it’s about to read a multiline command.
Using this blob, let’s tell bash it’s a multiline command:
Bash will now read the next lines by attaching a «>» for each new line.
Use Linux Reader to open Linux files
As promised, here’s the software to use and open Linux files on a Windows PC. DiskInternals Linux Reader is an intuitive software app with an easy-to-understand interface. It displays all the Linux partitions present on a Windows PC and lets you access the important files that are saved inside. Linux Reader works with different file systems: Ext2/3/4, ReiserFS, Reiser4, HFS, HFS+, FAT, exFAT, NTFS, ReFS, UFS2, RomFS(reader), ZFS (preview only*), XFS (preview only*), Hikvision NAS and DVR (preview only*).
Also, DiskInternals Linux Reader works perfectly for a virtual machine and dual-boot PCs. It is a handy app for all Linux operators. More interestingly, DiskInternals Linux Reader is free to download and use.
Related articles
Linux Shell ZSH Bash Perl Ext2/Ext3/Ext4 Arch Linux Systemd New Mount a drive Got error or failure How to
- About a bash date command
- Bash and sh: is there any difference
- About Bash Language
- Guide in Pictures
- Shell script usage
- Bash: A Script For User Input
- Here is everything you need to know about Bash for Windows
- Bash Script SSH: How to Use It
- How to use bash get script directory in Linux
- Copy command in shell scripts
- Learn about a bash error code
- 5 Basic Shell Script Examples for Your First Script
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Linux ZSH: the basic you need to know
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Basic of Grep in Linux Shell Script
- Bash: how to split strings in Linux
- An Algorithm: How to Create Bash While Loop
- Bash Time Command on Linux
- Bash: How to Check if the File Does Not Exist
- How to Use Shell Script Sleep Command
- Linux Shell: What You Need to Know at First
- Bash Script: All You Need to Know
- Mount Linux Drive on Windows for Free
- How to Mount Ext4 on Windows for Free
- How to Access Ext4 from Windows
- How to Access Linux Ext2 or Ext3 on Windows
- Linux: Sudo in Bash Scripts
- A Linux bin/bash Shell
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: Write a Shell Script
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- A Bash Test Command
- A Bash Status of Last Command
- Linux: New Line in Shell Script
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- A Bash Dirname Command
- Linux: A Bash Linter
- A Bash Nested Loop
- Use a Shell Script to Send an Email
- Hello World Script in Bash
- A crontab service shell script
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- AWK in a Bash Script
- Learn about systemd startup script
- About chaining bash commands
- Using a bash tee command
- Learn about useful bash scripts
- Bash for Loop in One Line
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash export command
- About a bash UNTIL loop
- Using /usr/bin/env command
- Learn about Korn shell scripting
- Shell Script: Replace String in File
- Linux: Bash String Ends With
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about C shell script
- Learn about SFTP in bash scripting
- Install Oracle Database
- Examples of using the Expect
- Learn to run Perl script in Linux
Split Long Bash Command into Multiple Lines in a Script
Bash Scripting is something every Linux nerd should strive to master. Scripts help us automate repetitive work and create custom tools.
However, in some instances, you may encounter a scenario where you have a long command. In bash, long commands do not affect the functionality of the tool. However, they are not easy to read.
In this quick tutorial, we shall discuss how to format a long command to span multiple lines. Splitting a long command into multiple lines makes the commands more readable and easy to edit.
How to Use Bash Backslash To Split Long Commands
To split long commands into readable commands that span multiple lines, we need to use the backslash character (\). The backslash character instructs bash to read the commands that follow line by line until it encounters an EOL.
The example below shows how to write a long command into multiple lines making it easier to read.
In the command above, we split multiple commands into individual lines. This way, we can see what each command is doing and modify it quickly if the need arises.
It is good to note that you can also use pipelines to split commands in the example above. However, this is not universally applicable as the following commands might not support input from pipes.
NOTE: Do not enclose the backslash in quotes or include whitespaces before it.
We can also apply the method above to a bash script. Using backslash, we can span a command into multiple lines making it more readable.
Here’s is an example use case:
#!/bin/bash
zstd -z \
—ultra \
-r —rm \
—format =zstd *
exit_code = $?
if [ $exit_code -eq 0 ] ; then
echo “Success”
else
echo “Fail”
fi
In the example above, we use backslash characters to span the options of the zstd command to multiple lines.
Conclusion
In this short tutorial, we discussed the basics of the backlash characters in bash and how we can span long commands into multiple lines. To learn more about bash and bash scripting, consider the documentation.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list
How to enter a multi-line command in bash on command line
I am troubleshooting a script and I wanted to manually enter each line, one by one, to find the error. My script has an if statement:
if [ -d dir1 ]; then rm -rf dir1 fi
$ if [ -d dir1 ]; then rm -rf dir1 fi >
I tried Ctrl-D but then get message : bash: syntax error: unexpected end of file So how do I enter this if statement on the command line?
3 Answers 3
Short answer: you need a semi-colon after rm -rf dir1 .
See it in action: enter the if block exactly as it appears in the script:
$ if [ -d dir1 ]; then > rm -rf dir1 > fi
After running it, use the up-arrow to call back the last command, it will show you the entire if block on one line:
$ if [ -d dir1 ]; then rm -rf dir1; fi
The shell needs some way to interpret breaks between commands inside the if-block. It can’t simply treat it as a single command executed all at once, so the semi-colons serve as those breaks.
Besides John’s answer, which is totally correct but will still end up with you entering a (very long) single-line command, there’s an actual way to enter multi-line commands:
$ if [ -d dir1 ]; then > rm -rf dir1 > fi
That is… just type. Upon Enter, the shell will see that the command is not complete and offer the “continuation prompt”.
But what if the command is actually complete, or you don’t want to rely on that? Also possible.
It’s called VLNEXT and usually set to ^V , but if you want to make really sure, check stty -a output and look for lnext in the cchars group (on BSD; GNU stty is a rather unsorted output but it’s in the third line for me).
As an added complication, in cooked mode, the Enter key submits ^M (ASCII 0x0D) but we actually need ASCII 0x0A which is ^J so you have to press that with VLNEXT.
Then, we can type if [ -d dir1 ]; then ^V ^J rm -rf dir1 ^V ^J fi Enter and it will run this as one big command. It looks like this in GNU bash…
$ if [ -d dir1 ]; then rm -rf dir1 fi
… and when pressing ↑ to check history, you’ll see it actually saved the one-line form with semicola there.
Other shells may differ in how they present it — for example, mksh will display it like this during input…
$ if [ -d dir1 ]; then^Jrm -rf dir1^Jfi
… and if you press ↑ you’ll see it saves it precisely like that in the history. If you press ← a few times, you’ll notice that these ^J there are treated as a single character, because it’s really a control character, not the two characters ^ (caret) and J .
So, GNU bash will have the nicer input but transform it internally while mksh has one-line input (and command line editing) but keeps it in the history exactly as typed.
In zsh it looks like multiline bash while inputting…
% if [ -d dir1 ]; then rm -rf dir1 fi
… and this is also stored in the history (like in mksh, but zsh has multi-line input editing and therefore can afford to move the cursor up and down the lines and so expand the ^J to an actual newline; this has the downside, though, that pressing ^U no longer kills the entire input but only one subline within).
yash behaves like 1999’s pdksh in that it inputs with the ^J on one line (like mksh does) but saves each line to the history individually, so on ↑ all you get is the last fi , which is probably very much not helpful.
A modern (Debian stable/bullseye) dash behaves like mksh here.
The modern AT&T ksh93 ksh “u+m” is even stranger: upon inputting, it behaves like its cousins from the pdksh/mksh lineage, but when pressing ↑ it fetches (only) the first line of the command (instead of the last like pdksh did), which is probably also not very helpful.
tcsh is… strange. For ^V ^J it shows ^M inline (but otherwise behaves like mksh except, of course, since it’s not a POSIX-compatible shell, the if command will fail with a syntax error), whereas, with both ^V ^M and ^V Enter , it behaves as if just Enter was pressed.
> if [ -d dir1 ]; then^Mrm -rf dir1^Mfi if: Expression Syntax. rm: Command not found.
% if [ -d dir1 ]; then^Jrm -rf dir1^Jfi if: Expression Syntax.
… has no surprises there. It does not have an interactive way to retrieve history, and issuing the history command to show the saved lines gives no output (not even for a simple ls ), so I believe it needs some setting to be enabled or something (I never used the family of C shells).
Full disclosure: I’m the mksh developer.