How to break a long string into multiple lines assigned to a variable in linux bash script

Clash Royale CLAN TAG#URR8PPP
I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple lines and assigned to a variable?
linux bash variable text-formatting
add a comment |
I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple lines and assigned to a variable?
linux bash variable text-formatting
2
Can you provide the data in your script and add it your question?
– Nasir Riley
Feb 28 at 15:28
1
What operation does the script perform on the variable? How are you splitting it? What is the error exactly?
– steeldriver
Feb 28 at 15:33
I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines.
– Srinivas Kamalanathan Attipatt
Feb 28 at 16:28
1
@SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question.
– steeldriver
Feb 28 at 17:11
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller♦
Mar 17 at 16:32
add a comment |
I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple lines and assigned to a variable?
linux bash variable text-formatting
I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple lines and assigned to a variable?
linux bash variable text-formatting
linux bash variable text-formatting
edited Feb 28 at 17:15
Rui F Ribeiro
41.8k1483142
41.8k1483142
asked Feb 28 at 15:25
Srinivas Kamalanathan AttipattSrinivas Kamalanathan Attipatt
1
1
2
Can you provide the data in your script and add it your question?
– Nasir Riley
Feb 28 at 15:28
1
What operation does the script perform on the variable? How are you splitting it? What is the error exactly?
– steeldriver
Feb 28 at 15:33
I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines.
– Srinivas Kamalanathan Attipatt
Feb 28 at 16:28
1
@SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question.
– steeldriver
Feb 28 at 17:11
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller♦
Mar 17 at 16:32
add a comment |
2
Can you provide the data in your script and add it your question?
– Nasir Riley
Feb 28 at 15:28
1
What operation does the script perform on the variable? How are you splitting it? What is the error exactly?
– steeldriver
Feb 28 at 15:33
I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines.
– Srinivas Kamalanathan Attipatt
Feb 28 at 16:28
1
@SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question.
– steeldriver
Feb 28 at 17:11
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller♦
Mar 17 at 16:32
2
2
Can you provide the data in your script and add it your question?
– Nasir Riley
Feb 28 at 15:28
Can you provide the data in your script and add it your question?
– Nasir Riley
Feb 28 at 15:28
1
1
What operation does the script perform on the variable? How are you splitting it? What is the error exactly?
– steeldriver
Feb 28 at 15:33
What operation does the script perform on the variable? How are you splitting it? What is the error exactly?
– steeldriver
Feb 28 at 15:33
I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines.
– Srinivas Kamalanathan Attipatt
Feb 28 at 16:28
I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines.
– Srinivas Kamalanathan Attipatt
Feb 28 at 16:28
1
1
@SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question.
– steeldriver
Feb 28 at 17:11
@SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question.
– steeldriver
Feb 28 at 17:11
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller♦
Mar 17 at 16:32
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller♦
Mar 17 at 16:32
add a comment |
3 Answers
3
active
oldest
votes
Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:
#!/bin/bash
text=(
'Contrary to popular'
'belief, Lorem Ipsum'
'is not simply'
'random text. It has'
'roots in a piece'
'of classical Latin'
'literature from 45'
'BC, making it over'
'2000 years old.'
)
# output one line per string in the array:
printf '%sn' "$text[@]"
# output all strings on a single line, delimited by space (first
# character of $IFS), and let "fmt" format it to 45 characters per line
printf '%sn' "$text[*]" | fmt -w 45
add a comment |
One suggestion:
x='Lorem ipsum dolor sit amet, consectetur '
'adipiscing elit, sed do eiusmod tempor '
'incididunt ut labore et dolore magna aliqua.'
Which results in the expected:
$ printf '%sn' "$x"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
add a comment |
Assuming you want to assign the string some long piece of text to assign to the variable str. This won't work:
str='some long'
'piece of text'
'to assign'
It'll try to run the lines after the first as commands, you'll probably get "command not found" errors.
You can do this, but the newlines will be embedded in the variable, so it won't be a single line:
str='some long
piece of text
to assign'
Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str="$str//$'n'/ " to do the replacement and save the new value in the same variable. Note that any trailing whitespace on all but the last line will be left in the string.
Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):
str='some long'
str+=' piece of text'
str+=' to assign'
Here, any whitespace will need to be manually typed within the quotes.
Or, similarly in a standard shell:
str='some long'
str="$str"' piece of text'
str="$str"' to assign'
Then there's the way with line continuation (that Jeff already mentioned in their answer):
str='some long'
' piece of text'
' to assign'
Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503572%2fhow-to-break-a-long-string-into-multiple-lines-assigned-to-a-variable-in-linux-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:
#!/bin/bash
text=(
'Contrary to popular'
'belief, Lorem Ipsum'
'is not simply'
'random text. It has'
'roots in a piece'
'of classical Latin'
'literature from 45'
'BC, making it over'
'2000 years old.'
)
# output one line per string in the array:
printf '%sn' "$text[@]"
# output all strings on a single line, delimited by space (first
# character of $IFS), and let "fmt" format it to 45 characters per line
printf '%sn' "$text[*]" | fmt -w 45
add a comment |
Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:
#!/bin/bash
text=(
'Contrary to popular'
'belief, Lorem Ipsum'
'is not simply'
'random text. It has'
'roots in a piece'
'of classical Latin'
'literature from 45'
'BC, making it over'
'2000 years old.'
)
# output one line per string in the array:
printf '%sn' "$text[@]"
# output all strings on a single line, delimited by space (first
# character of $IFS), and let "fmt" format it to 45 characters per line
printf '%sn' "$text[*]" | fmt -w 45
add a comment |
Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:
#!/bin/bash
text=(
'Contrary to popular'
'belief, Lorem Ipsum'
'is not simply'
'random text. It has'
'roots in a piece'
'of classical Latin'
'literature from 45'
'BC, making it over'
'2000 years old.'
)
# output one line per string in the array:
printf '%sn' "$text[@]"
# output all strings on a single line, delimited by space (first
# character of $IFS), and let "fmt" format it to 45 characters per line
printf '%sn' "$text[*]" | fmt -w 45
Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:
#!/bin/bash
text=(
'Contrary to popular'
'belief, Lorem Ipsum'
'is not simply'
'random text. It has'
'roots in a piece'
'of classical Latin'
'literature from 45'
'BC, making it over'
'2000 years old.'
)
# output one line per string in the array:
printf '%sn' "$text[@]"
# output all strings on a single line, delimited by space (first
# character of $IFS), and let "fmt" format it to 45 characters per line
printf '%sn' "$text[*]" | fmt -w 45
edited yesterday
answered Mar 18 at 9:59
Kusalananda♦Kusalananda
138k17258426
138k17258426
add a comment |
add a comment |
One suggestion:
x='Lorem ipsum dolor sit amet, consectetur '
'adipiscing elit, sed do eiusmod tempor '
'incididunt ut labore et dolore magna aliqua.'
Which results in the expected:
$ printf '%sn' "$x"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
add a comment |
One suggestion:
x='Lorem ipsum dolor sit amet, consectetur '
'adipiscing elit, sed do eiusmod tempor '
'incididunt ut labore et dolore magna aliqua.'
Which results in the expected:
$ printf '%sn' "$x"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
add a comment |
One suggestion:
x='Lorem ipsum dolor sit amet, consectetur '
'adipiscing elit, sed do eiusmod tempor '
'incididunt ut labore et dolore magna aliqua.'
Which results in the expected:
$ printf '%sn' "$x"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
One suggestion:
x='Lorem ipsum dolor sit amet, consectetur '
'adipiscing elit, sed do eiusmod tempor '
'incididunt ut labore et dolore magna aliqua.'
Which results in the expected:
$ printf '%sn' "$x"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
answered Feb 28 at 15:31
Jeff Schaller♦Jeff Schaller
44k1161142
44k1161142
add a comment |
add a comment |
Assuming you want to assign the string some long piece of text to assign to the variable str. This won't work:
str='some long'
'piece of text'
'to assign'
It'll try to run the lines after the first as commands, you'll probably get "command not found" errors.
You can do this, but the newlines will be embedded in the variable, so it won't be a single line:
str='some long
piece of text
to assign'
Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str="$str//$'n'/ " to do the replacement and save the new value in the same variable. Note that any trailing whitespace on all but the last line will be left in the string.
Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):
str='some long'
str+=' piece of text'
str+=' to assign'
Here, any whitespace will need to be manually typed within the quotes.
Or, similarly in a standard shell:
str='some long'
str="$str"' piece of text'
str="$str"' to assign'
Then there's the way with line continuation (that Jeff already mentioned in their answer):
str='some long'
' piece of text'
' to assign'
Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.
add a comment |
Assuming you want to assign the string some long piece of text to assign to the variable str. This won't work:
str='some long'
'piece of text'
'to assign'
It'll try to run the lines after the first as commands, you'll probably get "command not found" errors.
You can do this, but the newlines will be embedded in the variable, so it won't be a single line:
str='some long
piece of text
to assign'
Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str="$str//$'n'/ " to do the replacement and save the new value in the same variable. Note that any trailing whitespace on all but the last line will be left in the string.
Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):
str='some long'
str+=' piece of text'
str+=' to assign'
Here, any whitespace will need to be manually typed within the quotes.
Or, similarly in a standard shell:
str='some long'
str="$str"' piece of text'
str="$str"' to assign'
Then there's the way with line continuation (that Jeff already mentioned in their answer):
str='some long'
' piece of text'
' to assign'
Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.
add a comment |
Assuming you want to assign the string some long piece of text to assign to the variable str. This won't work:
str='some long'
'piece of text'
'to assign'
It'll try to run the lines after the first as commands, you'll probably get "command not found" errors.
You can do this, but the newlines will be embedded in the variable, so it won't be a single line:
str='some long
piece of text
to assign'
Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str="$str//$'n'/ " to do the replacement and save the new value in the same variable. Note that any trailing whitespace on all but the last line will be left in the string.
Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):
str='some long'
str+=' piece of text'
str+=' to assign'
Here, any whitespace will need to be manually typed within the quotes.
Or, similarly in a standard shell:
str='some long'
str="$str"' piece of text'
str="$str"' to assign'
Then there's the way with line continuation (that Jeff already mentioned in their answer):
str='some long'
' piece of text'
' to assign'
Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.
Assuming you want to assign the string some long piece of text to assign to the variable str. This won't work:
str='some long'
'piece of text'
'to assign'
It'll try to run the lines after the first as commands, you'll probably get "command not found" errors.
You can do this, but the newlines will be embedded in the variable, so it won't be a single line:
str='some long
piece of text
to assign'
Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str="$str//$'n'/ " to do the replacement and save the new value in the same variable. Note that any trailing whitespace on all but the last line will be left in the string.
Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):
str='some long'
str+=' piece of text'
str+=' to assign'
Here, any whitespace will need to be manually typed within the quotes.
Or, similarly in a standard shell:
str='some long'
str="$str"' piece of text'
str="$str"' to assign'
Then there's the way with line continuation (that Jeff already mentioned in their answer):
str='some long'
' piece of text'
' to assign'
Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.
answered Feb 28 at 17:52
ilkkachuilkkachu
62.8k10103180
62.8k10103180
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503572%2fhow-to-break-a-long-string-into-multiple-lines-assigned-to-a-variable-in-linux-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you provide the data in your script and add it your question?
– Nasir Riley
Feb 28 at 15:28
1
What operation does the script perform on the variable? How are you splitting it? What is the error exactly?
– steeldriver
Feb 28 at 15:33
I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines.
– Srinivas Kamalanathan Attipatt
Feb 28 at 16:28
1
@SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question.
– steeldriver
Feb 28 at 17:11
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller♦
Mar 17 at 16:32