Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: split long string argument to multiple lines?

Tags:

bash

multiline

Given a command that takes a single long string argument like:

mycommand -arg1 "very long string which does not fit on the screen" 

is it possible to somehow split it in a way similar to how separate arguments can be split with \.

I tried:

mycommand -arg1 "very \ long \ string \ which ..." 

but this doesn't work.

mycommand is an external command so cannot be modified to take single arguments.

like image 871
ccpizza Avatar asked Oct 18 '17 10:10

ccpizza


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do you type a multi line command in bash?

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.


1 Answers

You can assign your string to a variable like this:

long_arg="my very long string\  which does not fit\  on the screen" 

Then just use the variable:

mycommand "$long_arg" 

Within double quotes, a newline preceded by a backslash is removed. Note that all the other white space in the string is significant, i.e. it will be present in the variable.

like image 142
Tom Fenech Avatar answered Sep 24 '22 00:09

Tom Fenech