How to Paste Multi-Line Bash Codes into Terminal and Run It All at Once

How do I paste multi-line bash codes into terminal and run it all at once?

Try putting \ at the end of each line before copying it.

Example:

echo "Hello world" && \
script_b.sh

echo $?

The exit code ($?) is now the full sequence of commands, and not just the last command.

how can I enter multiple lines in an OSX terminal?

End your line with a backslash (\), hit Enter, and continue typing on the next line.

Keep in mind that if you need spaces between the parts of your command, you need to enter them explicitly, e.g.:

ls \
-la

WT: Paste multiple lines to Windows Terminal without executing

The solution for me was to just comment out a line in the terminal setting.json with { "command": "paste", "keys": "ctrl+v" }

Sample Image

And then it works - here the expected output

working output

Edit:

After uncommenting the Ctrl+V chord in the settings.json, the paste functionality is ensured by the PSReadLine module (see Get-PSReadLineKeyHandler -Chord ctrl+v) but Ctrl+V will stop to work in other shells - use Shift+Ctr+V instead

How can I write multi-line code in the Terminal use python?

Just copy the code and past it in the terminal, and press return. This code works perfect if you do that:

   i = 0 
..
.. while i < 10:
.. i += 1
.. print(i)
..

1
2
3
4
5
6
7
8
9
10

Multi-line bash commands in makefile

You can use backslash for line continuation. However note that the shell receives the whole command concatenated into a single line, so you also need to terminate some of the lines with a semicolon:

foo:
for i in `find`; \
do \
all="$$all $$i"; \
done; \
gcc $$all

But if you just want to take the whole list returned by the find invocation and pass it to gcc, you actually don't necessarily need a multiline command:

foo:
gcc `find`

Or, using a more shell-conventional $(command) approach (notice the $ escaping though):

foo:
gcc $$(find)

Readline type wrapper that scrolls entire multiline commands at one time

From the rlwrap manual:

-m, --multi-line [newline_substitute]
Enable multi-line input using a "newline substitute" character
sequence (" \ ", [space-backslash-space] by default). Newline sub‐
stitutes are translated to newlines before sending the input to
command. With this option, you can call an external editor
$RLWRAP_EDITOR on the (expanded) current input with the
rlwrap_call_editor key (CTRL-^ by default)

When using rlwrap -m, pressing CTRL+^ will drop you into an editor where you can edit your multi-line snippet. Upon exiting the editor, the snippet will be sent to the rlwrapped command, and also be put into the rlwrap history as one single line (with newlines replaced by the "newline substitute") Moving through history with and will still show those snippets as one single line, but you can expand them at any moment into multi-line text within your editor by pressing CTRL+^ again

Not exactly what you're looking for, but it does scroll entire multiline commands at a time, which I like better than e.g. ipython where you still (at least with the terminals I use) have to press a couple times to move past a large function definition.

Of course, as with any readline wrapper, there are donwsides: you will lose any context-sensitive completion your CLI might have had, and even the paltry rlwrap completion mechanism will not work within the multi-line-editor.

Setup:

It should work "out of the box" by just issuing

$ rlwrap -m -a <my_cli_command> # -a because your cli probably 
# does some line editing already

Some tweaks:

  • If you prefer a diffferent editor, add a line

    export RLWRAP_EDITOR='<my_editor_of_choice>[-opts] in your .bashrc. The default is vi +%L. %F, %L and %C in RLWRAP_EDITOR will be replaced by filename, line and column, respectively, in order to drop you into the editor at the very spot where the cursor was in rlwrap
  • To use a different editing hotkey, add a line like "\C-b":rlwrap-call-editor to ~/.inputrc (CTRL+^ is the default)
  • To use a different "newline substitute" (e.g. "||") , add an argument to -m like in rlwrap -m'||' (the default is ' \ ')
  • if your editor_of_choice does syntax colouring based on filename extensions, add an argument -M.ext (default: no extension)

For example:

export RLWRAP_EDITOR='my_edit --auto-save-on-exit %F:%L'
# ... or put the above in .bashrc
rlwrap -a -m': ' -M_bas QLSuperBasic # ... those were the days

Spark shell : How to copy multiline inside?

just save your code to text file and use :load <path_to_your_script> in spark-shell

How to read multiple line of text from console and then save the content to a file with line break?

If you have bash on your AIX, then you can try this:

#!/bin/bash
echo "Paste the Bulletin:"
while IFS= read -r -t 2 c
do
keyvariable+="$c"$'\n'
done
echo "$keyvariable" >abc.txt
echo "Thanks!"


Related Topics



Leave a reply



Submit