- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company

Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Concatenating two string variables in bash appending newline
I have a variable final_list which is appended by a variable url in a loop as:
To my surprise the \n is appended as an space, so the result is:
while I wanted:
What is wrong?
3 Answers 3
New lines are very much there in the variable "$final_list" . echo it like this with double quotes :
OR better use printf :
- Thank you. Can you explain a bit on what is happening. – Aman Deep Gautam Aug 30, 2013 at 14:59
- 3 Quoting is of utmost importance in Unix shells. Without quotes shell doesn't apply any expansion rules and treats new lines as space. – anubhava Aug 30, 2013 at 15:01
- 4 Without the quotes, any white space (including newlines) in the expansion of $final_list is treated as a word separator, not literal characters to print. echo receives only 3 arguments, the actual URLs. With quotes, echo receives a single argument, the string that contains urls and the newlines that separate them. – chepner Aug 30, 2013 at 16:28
It may depend on how you're trying to display the final result. Try outputting the resulting variable within double-quotes:
Adding one more possible option if anyone like to try.
With String Value :
Command : echo "This is the string I want to print in next line" | tr " " "\n"
With Vars used :
if you like to have few words to be in one line and few in next line, try with different separater. (like - or : or ; or etc...)
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged linux bash shell or ask your own question .
- The Overflow Blog
- Developers think AI assistants will be everywhere, but aren’t sure how to...
- Visible APIs get reused, not reinvented sponsored post
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- The Stack Exchange reputation system: What's working? What's not?
- Launching the CI/CD and R Collectives and community editing features for...
- The [amazon] tag is being burninated
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- Temporary policy: ChatGPT is banned
Hot Network Questions
- Best way to highlight the main result in a mathematical paper
- Short film involving a young man who has died and is performing good deeds to score enough points to reach Heaven
- Can LSTM model use ReLU or LeakyReLU as the activation funtion?
- Including somebody with an unrelated degree as a coauthor?
- split/reference big file by offset reference
- Is the cabin pressure "worse" at the back of the cabin than in front?
- Do MEMS or optical gyroscope record Earth's rotational and revolutional angular velocities?
- What to do when you find out a client went to another designer to change the finished design?
- Is "throw in an ape" an expression?
- How can I improve the colour of my red wine mousse?
- Why sulfites are often ignored in soil studies?
- Can magnetic fields be produced in other ways besides from a moving charge?
- Why are Deligne-type exponential sum estimates so hard to use?
- Hotkey for switching between pan and add point in QGIS georeferencer
- Could a Dragon use an unconscious PC as a thrown weapon?
- Meaning of も in 3つ も
- Was Freemasonry such a big problem in 1980s UK policing?
- How can I protect /dev/sdX against accidental formatting?
- plot diagonal lattice path
- What if you could shut off the pull of gravity on your body?
- Would a spider familiar be able to press the button on the immovable rod whilst swinging from a rope tied to said rod?
- separating consciousness-feeling-perception
- Why are most US news programs silent about Iran-Saudi deal announced at Beijing on March 10th?
- Extracting list elements following a specific marker
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Stack Exchange Network
Stack Exchange network consists of 181 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How to add newlines into variables in bash script
I get the \n printed out too. How can I have newlines then?

- 1 While the answers here are great, in reality I think you'd be better off using an array for this sort of thing most of the time. – evilsoup Jun 10, 2013 at 8:49
- See also the question Trying to embed newline in a variable in bash . – HelloGoodbye May 21, 2015 at 8:26
9 Answers 9
In bash you can use the syntax
Single quotes preceded by a $ is a new syntax that allows to insert escape sequences in strings.
Also printf builtin allows to save the resulting output to a variable
Both solutions do not require a subshell.
If in the following you need to print the string, you should use double quotes, like in the following example:
because when you print the string without quotes, newline are converted to spaces.
- 2 What is the syntax str=$'Hello World\n===========\n' called? variable substitution? – zengr Aug 10, 2013 at 0:28
- 10 @zengr: It's called ANSI-C quoting , and it's also supported in zsh and ksh ; however, it is NOT POSIX-compliant. – mklement0 Jul 8, 2014 at 14:46
- 6 @mkelement0, it comes from ksh93, is also supported by zsh, bash, mksh and FreeBSD sh, and its inclusion in the next major revision of POSIX is under discussion – Stéphane Chazelas Sep 26, 2016 at 9:42
- 4 Doesn't seem to work with double quotes? e.g. str=$"My $PET eats:\n$PET food" ? This approach works for double quotes – Brad Parks Sep 26, 2019 at 12:33
You can put literal newlines within single quotes (in any Bourne/POSIX-style shell).
For a multiline string, here documents are often convenient. The string is fed as input to a command.
If you want to store the string in a variable, use the cat command in a command substitution. The newline character(s) at the end of the string will be stripped by the command substitution. If you want to retain the final newlines, put a stopper at the end and strip it away afterward. In POSIX-compliant shells, you can write str=$(cat <<'EOF'); str=${str%a} followed by the heredoc proper, but bash requires the heredoc to appear before the closing parenthesis.
In ksh, bash and zsh, you can use the $'…' quoted form to expand backslash escapes inside the quotes.
- 1 I'm using GNU bash 4.1.5, and str=$(cat <<'EOF') doesn't work as-is.. The ) needs to be placed on the next line after the end-of-doc EOF .. but even so, it loses the trailing newline due to Command Substitution. – Peter.O Sep 3, 2011 at 16:49
- @fred Good points, I've explained about the trailing newlines and shown code that works in bash. I think it's a bug in bash, though to be honest upon rereading the POSIX spec I find it unclear that the behavior is mandated when the << is inside a command substitution and the heredoc isn't. – Gilles 'SO- stop being evil' Sep 4, 2011 at 17:01
- Great stuff; to preserve trailing (and leading) \n instances in bash when capturing a here-doc in a variable, consider IFS= read -r -d '' str <<'EOF'... as an alternative to the stopper approach (see my answer). – mklement0 Jul 8, 2014 at 15:29
- The very first example doesn't work in my bash. It results in str=Hello World – Mike B Sep 15, 2020 at 20:37
- 1 @mblakesley It does work. You've made a mistake somewhere. – Gilles 'SO- stop being evil' Sep 15, 2020 at 20:48
If you need newlines in your script many times you could declare a global variable holding a newline. That way you can use it in double-quoted strings (variable expansions).
- Why would $'' require a subshell? – Mat Jun 10, 2013 at 7:43
- Sorry, I misread an answer. – pihentagy Jun 10, 2013 at 11:19
- Could I ask for explanation from downvoters? – pihentagy Jan 23, 2017 at 10:12
- Nice! This can be included in variables using double quotes, e.g. "My dog eats:${NL}dog food" – Brad Parks Sep 26, 2019 at 12:30
- This worked for me. When using a bash script to composite a string that gets automatically copied onto the clipboard for subsequent pasting elsewhere, this approach worked for adding newlines in the resulting output. – Ville Oct 28, 2019 at 22:36
Are you using "echo"? Try "echo -e".
- 2 BTW. You don't need the final \n, because echo will automatically add one, unless you specify -n. (However, the main brunt of the question, is how to get these newlines into a variable). – Peter.O Sep 3, 2011 at 8:48
- +1 Of all the solutions, this one is the most direct and simplest. – Hai Vu Sep 3, 2011 at 16:11
- echo -e doesn't work in OS X – Greg M. Krsak Feb 4, 2014 at 20:21
- 2 @GregKrsak: In bash , echo -e does work on OS X - that's because echo is a bash builtin (rather than an external executable) and that builtin does support -e . (As a builtin, it should work on all platforms that bash runs on; incidentally, echo -e works in ksh and zsh too). By contrast, however, the external echo utility on OS X - /bin/echo - indeed does not support -e . – mklement0 Jul 8, 2014 at 14:54
From all discussion, here is the simplest way for me:
The echo command must use double quotes .
To complement the great existing answers:
If you're using bash and you prefer using actual newlines for readability , read is another option for capturing a here-doc in a variable , which (like other solutions here) doesn't require use of a subshell.
-r ensures that read doesn't interpret the input (by default, it would treat backslashes special, but that is rarely needed).
-d '' sets the "record" delimiter to an empty string, causing read to read the entire input at once (instead of just a single line).
Note that by leaving $IFS (the internal field separator) at its default, $' \t\n' (a space, a tab, a newline), any leading and trailing whitespace is trimmed from the value assigned to $str , which includes the here-doc's trailing newline. (Note that even though the here-doc's body starts on the line after the start delimiter ( 'EOF' here), it does not contain a leading newline).
Usually, this is the desired behavior, but if you do want that trailing newline, use IFS= read -r -d '' instead of just read -r -d '' , but note that any leading and trailing whitespace is then preserved. (Note that prepending IFS= directly to the read command means that the assignment is in effect during that command only, so there is no need to restore the previous value.)
Using a here-doc also allows you to optionally use indentation to set off the multiline string for readability:
Placing - between << and the opening here-doc delimiter ( 'EOF' , here) causes leading tab characters to be stripped from the here-doc body and even the closing delimiter, but do note that this only works with actual tab characters , not spaces, so if your editor translates tab keypresses into spaces, extra work is needed.
- 3 Why not simply result+=$foo$'\n' ? $(printf %s "$foo") would trim the trailing newline characters in $foo if any. – Stéphane Chazelas Sep 26, 2016 at 9:50
- 1 There is no explanation for the code, else it looks fine to me. – somethingSomething Jul 17, 2019 at 5:54
- This makes sense when the printf format is more complex than just '%s' . It's the best way I've seen so far to solve my problem. – rfmodulator Sep 2, 2020 at 0:09
The first comment on the question mentions arrays, but nobody shows how to do it with arrays, so here it is.
The first line creates an array with one element. Here the quotes are necessary (single quotes work too), otherwise the two words would be added as two separate array elements.
The second line adds one more element to the array with the += operator. Here the quotes are optional, because there is no whitespace.
The third line prints out each array element as string ( %s ) followed by a newline ( \n ).
There can be whitespace around the array elements, on the first line I added spaces between the string and brackets, on the second I omitted spaces, to show it works both ways.
you need to do it this way:
As Fred pointed it out, this way you will loose trailing "\n". To assign variable with backslash sequences expanded, do:
let's test it:
gives us now:
Note, that $'' is different than $"". The second does translation according to current locale. For deital see QUOTING section in man bash .
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged bash quoting or ask your own question .
- The Overflow Blog
- Building an API is half the battle: Q&A with Marco Palladino from Kong
- Developers think AI assistants will be everywhere, but aren’t sure how to...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- The Stack Exchange reputation system: What's working? What's not?
Hot Network Questions
- Can we explain why using `Nothing` twice on a list does not operate twice?
- broker cancelled life insurance policy without authorization
- Epistemic failure and blaming others
- Is it easier to unclip using a shoe with higher stiffness index?
- Recommendations for getting into sheaves with emphasis on differential geometry and algebraic topology
- Called Dr. in government setting?
- A melody is built from both notes and chords
- Is there a "Standard Algorithm" language, as used in academic papers?
- Is there really a bawdy pun at the conclusion of Romeo and Juliet?
- Order of the nations
- H-Bridge blows the fuses in my house
- Do MEMS or optical gyroscope record Earth's rotational and revolutional angular velocities?
- What's the best word to indicate "not mindless"?
- How can I protect /dev/sdX against accidental formatting?
- Including somebody with an unrelated degree as a coauthor?
- Relationship between z-score and the normal distribution
- Did mechanical hard drives often malfunction in high elevation places such as Bogota?
- 14 "Trashed" bikes acquired for free. Worth repairing and reselling? What do I look for?
- Portable Alternatives to Traditional Keyboard/Mouse Input
- Why isn't the derivative of the volume of the cone its surface area?
- Is it legal for a company to require you to delete your account to unsubscribe from marketing emails?
- Hotkey for switching between pan and add point in QGIS georeferencer
- Why are most US news programs silent about Iran-Saudi deal announced at Beijing on March 10th?
- Tools/techniques to remove a long section from a wooden board
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

Bash string concatenate possible? [SOLVED]
Table of Contents
How to concatenate strings with underscore, newline, whitespace or any other character in bash? How to append substrings in a string in bash or shell script? How to append starings in a variable using for loop with whitespace? Bash join strings with separator.
These are some of the common questions which we will address in this tutorial. concatenate means nothing but linking or putting things together in some sort of chain or series. You may choose to put two or multiple strings together by any separation character.
Basic concatenation of two strings with no separator
This is quite a straight forward use case wherein we have two variables with some string and you want to concatenate them
For example, I have two variables:
As you have noticed VAR1 already contains an extra space so we are just printing both variables together, the output from this script would be:
Now we can also join VAR1 and VAR2 into a third variable VAR3 and the output would be the same i.e. " Hello World "
Join strings with special character as separator
Now the above was a very basic example, let' take it to the next level. In this example we will concatenate strings with underscore, you may choose any other separator such as comma, whitespace, hyphen etc.
Using above script as an example, what would happen if I just place " _ " (underscore) between both the variables, it should work right?
Let's check the output from this script:
From our output "Hello" is missing, WHY?
Because bash considered $VAR_ as one variable and $VAR2 as second. Since $VAR_ doesn't exist so Hello is missing. So in such case always use curly braces to denote a variable when working with concatenation.
So as you see now I have used curly braces {} to make sure the separator is not considered part of the variable, now let's check the output from the script:
This is the one of the most important thing you should always remember when working with bash string concatenation. Now you can use any other special character here to combine both the strings.
Concatenate in a loop (append strings to a variable)
Now assuming you have a requirement where in you have to test network connectivity of multiple hosts and then print a summary with list of success and failed hosts. This would require you to keep storing the list of success and failed hosts, this can be done using various methods but we will use += operator to store the list of strings in a variable (or convert to array based on your requirement)
In this example script I have defined a list of 4 hosts out of which 2 are reachable ( yeah I know this already ) while two are un-reachable but we will use a script to check this. Based on the ping output we will append the string in different variable
Output from this script:
Here if you notice this if condition is the main part which stores the server list. I have intentionally added an empty whitespace after $server so that there is a gap between list of servers when added to SUCCESS or FAILED
You may choose to strip the last whitespace character at the end of execution of that creates a problem in your output:
Concatenate strings using new line character
You can use the same operator += to append strings with new line character, although printing the output would require certain extra handling . In this example I have created an array with some values, I will iterate over these values and then join them together with a new line character at the end
So I have combined all the strings in a for loop (you can also use while or any other loop - doesn't matter ) using new line character. By default echo will not be able to understand " \n " and will consider it as a text so we use -e to enable interpretation of backslash escapes. The -n is used with echo to suppress the last line or you will have an extra new line in the end, you can also use printf to avoid all this additional argument.
In this tutorial we learned about bash string concatenation using different joining character such as whitespace, newline, special characters etc. You can use += operator in all sorts of scenarios to combine strings. But one thing to remember is that by default in a loop += will append the string in the end of variable, if you have a different requirement than that would need special handling.
Didn't find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Leave a Comment Cancel reply
Save my name and email in this browser for the next time I comment.
Notify me via e-mail if anyone answers my comment.
Shell Programming and Scripting
Concat 2 variables with newline.

10 More Discussions You Might Find Interesting
1. unix for advanced & expert users, concat data, discussion started by: kiranparsha, 2. shell programming and scripting, concat name, discussion started by: xitrum, 3. shell programming and scripting, how to concat columns, discussion started by: vikram_tanwar12, 4. shell programming and scripting, discussion started by: kmsekhar, 5. unix for dummies questions & answers, parse or cut concat variables to individual values, discussion started by: lynnc, 6. shell programming and scripting, discussion started by: posner, 7. shell programming and scripting, append newline to existing variables, discussion started by: finalight, 8. shell programming and scripting, concat strings, discussion started by: melanie_pfefer, 9. shell programming and scripting, concat string, discussion started by: mpang_, 10. shell programming and scripting, discussion started by: dhananjaysk, member badges and information modal, featured tech videos.

How to Concatenate String Variables in Bash [Join Strings]

Two or more string variables of characters or numbers or a mix of characters and numbers can be concatenated easily in bash. The string variables are required to concatenate for different types of programming tasks. Many ways exist in bash to concatenate string variables.
In this tutorial, we learn different ways of concatenating string variables in bash.
Table of Contents
What is String Concatenation?
1. concatenating variables with string literals.
The string variables and string literal can be concatenated by using a double quotation in the `echo` command. The following script will concatenate two string variables with the string literal.

Using the script we have concatenated the two string variables with value ‘Bash’ and ‘Script’ to print the results as “Learn Bash Script”. Basically, we placed the two variable values into a sentence.
2. Concatenating variables of string and number
The numeric value is converted into a string value when concatenated with a string value. The following script will concatenate a string variable with a number variable. The `echo` command has been used to print the concatenated string as in the previous example.

In the script we have concatenated one string variable with the value, 'Logitech Keyboard' and the number variable with the value, 1078.
3. Concatenating variables without any separator
The following script will concatenate three string variables without any separator. The `echo` command has been used to print the concatenated string like in the previous example.

Here we have concatenated three string variables with the values, 'Tic-', 'Tac-', and 'Toe'.
5. Concatenating variables with a separator

Using the script we have concatenated three string variables with the values, 'Linux', 'Windows', and 'Mac' by adding a separator, ':'.
6. Concatenating variables with shorthand operator
The following script will take three input values from the user and concatenate three values with another string by using a shorthand operator (+=). The values of the string variables will be added at the end of the content of the string variable that has been used on the left side of the operator. The `echo` command has been used to print the concatenated string as in the previous example.

In the script we have concatenated three string variables with the values, 'Bash', 'Python', and 'Perl' by using the shorthand operator, '+='.
7. Concatenating variables with a newline
You can use newline(\n) as a separator to concatenate string variables in bash. The following script will concatenate two string variables by using newline(\n) and the value of each variable will be printed in each line. The `echo` command has been used to print the concatenated string as in the previous example.

You can see we have concatenated two string variables with the values, '07856', and 'Nehal Sharif' by adding newline(\n).
8. Concatenating variables with a special character

Using the script we have concatenated one string variable with the value, 'Bash quick reference' and the number variable with the value, 6 by adding a special character, '$'.
9. Concatenating string variables by printf
The following script will concatenate two string variables with a hyphen (-) in the script. Here, the `printf` command has been used to print the concatenated string. The formatting specifier, “%s” has been used in the printf command for denoting string value.

In the example script we have concatenated two string variables with the value, 'CSE407' and 'Unix' by adding hyphen(-).
String concatenation tasks have been shown here by using multiple bash scripts. You can follow any of the ways shown in this tutorial for concatenating string variables based on your programming requirements.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Sorry about that.
Leave a Reply
Leave a comment cancel reply.

Bash string with newline
Oct 19, 2020 • blog • edit.
This post goes over how to include newlines in a Bash string.
If you’re using Bash :
You may notice that newline characters are escaped in strings:
To preserve the newline in echo , you can set option -e :
Alternatively, you can assign the newline character to a variable :
And then substitute it via parameter expansion :
Make sure to use double quotes "..." otherwise the variable reference won’t be expanded.
Please support this site and join our Discord !

Bash One-Liners Explained, Part II: Working with strings
This is the second part of the Bash One-Liners Explained article series. In this part I'll show you how to do various string manipulations with bash. I'll use only the best bash practices, various bash idioms and tricks. I want to illustrate how to get various tasks done with just bash built-in commands and bash programming language constructs.
See the first part of the series for introduction. After I'm done with the series I'll release an ebook (similar to my ebooks on awk , sed , and perl ), and also bash1line.txt (similar to my perl1line.txt ).
Also see my other articles about working fast in bash from 2007 and 2008:
- Working Productively in Bash's Emacs Command Line Editing Mode (comes with a cheat sheet)
- Working Productively in Bash's Vi Command Line Editing Mode (comes with a cheat sheet)
- The Definitive Guide to Bash Command Line History (comes with a cheat sheet)
Let's start.
Part II: Working With Strings
1. Generate the alphabet from a-z
This one-liner uses brace expansion. Brace expansion is a mechanism for generating arbitrary strings. This one-liner uses a sequence expression of the form {x..y}, where x and y are single characters. The sequence expression expands to each character lexicographically between x and y, inclusive.
If you run it, you get all the letters from a-z:
2. Generate the alphabet from a-z without spaces between characters
This is an awesome bash trick that 99.99% bash users don't know about. If you supply a list of items to the printf function it actually applies the format in a loop until the list is empty! printf as a loop! There is nothing more awesome than that!
In this one-liner the printf format is "%c" , which means "a character" and the arguments are all letters from a-z separated by space. So what printf does is it iterates over the list outputting each character after character until it runs out of letters.
Here is the output if you run it:
This output is without a terminating newline because the format string was "%c" and it doesn't include \n . To have it newline terminated, just add $'\n' to the list of chars to print:
$'\n' is bash idiomatic way to represent a newline character. printf then just prints chars a to z, and the newline character.
Another way to add a trailing newline character is to echo the output of printf:
This one-liner uses command substitution, which runs printf "%c" {a..z} and replaces the command with its output. Then echo prints this output and adds a newline itself.
Want to output all letters in a column instead? Add a newline after each character!
Want to put the output from printf in a variable quickly? Use the -v argument:
This puts abcdefghijklmnopqrstuvwxyz in the $alphabet variable.
Similarly you can generate a list of numbers. Let's say from 1 to 100:
Alternatively, if you forget this method, you can use the external seq utility to generate a sequence of numbers:
3. Pad numbers 0 to 9 with a leading zero
Here we use the looping abilities of printf again. This time the format is "%02d " , which means "zero pad the integer up to two positions", and the items to loop through are the numbers 0-9, generated by the brace expansion (as explained in the previous one-liner).
If you use bash 4, you can do the same with the new feature of brace expansion:
Older bashes don't have this feature.
4. Produce 30 English words
This is an abuse of brace expansion. Just look at what this produces:
Crazy awesome!
Here is how it works - you can produce permutations of words/symbols with brace expansion. For example, if you do this,
It will produce the result a1 a2 a3 b1 b2 b3 c1 c2 c3 . It takes the first a , and combines it with {1,2,3} , producing a1 a2 a3 . Then it takes b and combines it with {1,2,3} , and then it does the same for c .
So this one-liner is just a smart combination of braces that when expanded produce all these English words!
5. Produce 10 copies of the same string
This one-liner uses the brace expansion again. What happens here is foo gets combined with 10 empty strings, so the output is 10 copies of foo :
6. Join two strings
This one-liner simply concatenates two variables together. If the variable x contains foo and y contains bar then the result is foobar .
Notice that "$x$y" were quoted. If we didn't quote it, echo would interpret the $x$y as regular arguments, and would first try to parse them to see if they contain command line switches. So if $x contains something beginning with - , it would be a command line argument rather than an argument to echo:
Versus the correct way:
If you need to put the two joined strings in a variable, you can omit the quotes:
7. Split a string on a given character
Let's say you have a string foo-bar-baz in the variable $str and you want to split it on the dash and iterate over it. You can simply combine IFS with read to do it:
Here we use the read x command that reads data from stdin and puts the data in the x y z variables. We set IFS to - as this variable is used for field splitting. If multiple variable names are specified to read , IFS is used to split the line of input so that each variable gets a single field of the input.
In this one-liner $x gets foo , $y gets bar , $z gets baz .
Also notice the use of <<< operator. This is the here-string operator that allows strings to be passed to stdin of commands easily. In this case string $str is passed as stdin to read .
You can also put the split fields and put them in an array:
The -a argument to read makes it put the split words in the given array. In this case the array is parts . You can access array elements through ${parts[0]} , ${parts[1]} , and ${parts[0]} . Or just access all of them through code>${parts[@]}</code .
8. Process a string character by character
Here we use the -n1 argument to read command to make it read the input character at a time. Similarly we can use -n2 to read two chars at a time, etc.
9. Replace "foo" with "bar" in a string
This one-liner uses parameter expansion of form ${var/find/replace} . It finds the string find in var and replaces it with replace . Really simple!
To replace all occurrences of "foo" with "bar", use the ${var//find/replace} form:
10. Check if a string matches a pattern
Here the one-liner does something if $file matches .zip . This is a simple glob pattern matching, and you can use symbols ? [...] to do matching. Code * matches any string, ? matches a single char, and [...] matches any character in ... or a character class.
Here is another example that matches if answer is Y or y :
11. Check if a string matches a regular expression
This one-liner tests if the string $str matches regex [0-9]+.[0-9]+ , which means match a number followed by a dot followed by number. The format for regular expressions is described in man 3 regex .
12. Find the length of the string
Here we use parameter expansion ${#str} which returns the length of the string in variable str . Really simple.
13. Extract a substring from a string
This one-liner extracts world from hello world . It uses the substring expansion. In general substring expansion looks like ${var:offset:length} , and it extracts length characters from var starting at index offset . In our one-liner we omit the length that makes it extract all characters starting at offset 6 .
Here is another example:
14. Uppercase a string
The declare command in bash declares variables and/or gives them attributes. In this case we give the variable var attribute -u , which upper-cases its content whenever it gets assigned something. Now if you echo it, the contents will be upper-cased:
Note that -u argument was introduced in bash 4. Similarly you can use another feature of bash 4, which is the ${var^^} parameter expansion that upper-cases a string in var :
15. Lowercase a string
Similar to the previous one-liner, -l argument to declare sets the lower-case attribute on var , which makes it always be lower-case:
The -l argument is also available only in bash 4 and later.
Another way to lowercase a string is to use ${var,,} parameter expansion:
Enjoy the article and let me know in the comments what you think about it! If you think that I forgot some interesting bash one-liners related to string operations, let me know in the comments also!
Dec 21, 2022
How to Concatenate Strings in Bash: A Guide for Connecting String Variables
The majority of programming languages can connect two or more strings. One programming language that makes it effortless to concatenate variables is bash .
What makes bash special is that string variables can be connected without the use of dedicated commands or functions. In other words, to combine string data, users can use simple variable manipulation or apply the same logic with the addition assignment operator (+=) .
In this tutorial, we will explain bash scripting, go over what bash concatenate strings are, and provide a variety of ways to concatenate strings.


What Is Bash Scripting?
Bash shell scripting allows users to execute hundreds of Linux commands with a single script instead of writing them all down one by one. It is extra helpful for users looking to automate their physical or virtual private servers (VPS) and increase productivity.
For example, any command a user can run natively on a terminal can be put into a bash script. This also applies to functions, so instead of writing them down every time, users only need to write a function once and reuse it in any bash scripts.
Any script starts with a .sh file and contains a similar structure:
The first line tells the terminal to run the script using bash exclusively, and all the following lines are the actual script itself.
In this particular example, the script created a new variable named mybashvariable and gave it a “ Hello, World ” value, and the script will print the value out.

What Is Concatenation in Linux?
Concatenation operation in bash is the process of attaching a string to the end of another string. Bash allows its users to concatenate strings by writing strings one after the other or joining them using the += operator.
String Concatenation – Adding One String Variable After the Other
The simplest string concatenation method is adding one string variable after the other. The following sections will show three different ways to do just that.
String Concatenation Using Literal Strings
Literal strings are printed literally, and there are two ways to print out a string literal – using singular quotes or a backlash symbol with regular double quotes. For example, we will create a new literal string variable without quotes and echo it:
In this case, the result would be:
Now, when we add singular or double quotes to the string variable name, the echo command will print the value literally:
Here’s the result:
Next, we will apply this logic to concatenate two strings:
We can also cover the last line’s variable using rounded brackets in order to guard it. Curly brackets are helpful if you got a variety of variables:
In both cases, the result will be shown as:
String Concatenation of Multiple Variables
Multiple string variables can be easily joined together with clear-cut variable manipulation.
For example, in the following bash script, we will use three different variables to create combined values. The echo command will subsequently print out the string data:
Here’s what the result will look like:
String Concatenation of Numbers and Strings
Bash allows its users to concatenate one or more variables that are not string-type. For this reason, it is possible to concatenate multiple variables, which can be strings or numbers:

String Concatenation Using the += Operator
Another way to join two or more strings to make a concatenated string is to use the addition assignment operator (+=). This operator makes it possible to connect strings using one or more variables.
For example, the following script can be used to join two strings with the use of a single variable:

A similar result can be achieved using two variables:

Concatenating Numeric Strings
The append operator method can also be used exclusively to append numeric string variables.

However, if you would like to add the numbers together, this logic needs to be used:

Concatenating Strings Using Bash for Loop
A more advanced way of using the bash concatenate functionality is implementing it into the bash for loop .
In the following example, we got a myvariable with three strings and a variable named results with an empty string. With the help of the bash for loop , we will be able to combine strings from myvariable with our string:

The bash programming language is a convenient and efficient tool for various variable manipulation actions. One of the most important examples is the ability to join different string variables into one.
In this tutorial, we’ve gone through the definition of bash scripting and concatenation. We also learned how to join string variables with two different methods.
We hope you found this tutorial useful. If you have any further questions, feel free to leave them in the comments section below.

Ignas takes great satisfaction in helping people tackle even the most complex technical issues. His current goal is to write easy-to-follow articles so that these issues will not happen at all. During his free time, Ignas likes to play video games and fix up things around his house.
Related tutorials

22 Feb • VPS •
Rocky Linux Review: Important Factors to Consider Before Migrating
Rocky Linux is a binary-compatible operating system based on the Red Hat Enterprise Linux (RHEL) source code. It is regarded as the unofficial...
17 Feb • VPS •
tmux Config: Understanding the Configuration File + Customization Examples
tmux is a popular terminal multiplexer that lets you run multiple sessions inside a single window. What is more, users can easily switch between these...
By Ignas R.
25 Oct • VPS •
7 Distributions to Serve as the CentOS Replacement
CentOS is arguably one of the best Linux distributions for virtual private servers (VPS). It is an open-source system with full Red Hat Enterprise...
By Noviantika G.
What our customers say
Leave a reply cancel reply.
By using this form you agree that your personal data would be processed in accordance with our Privacy Policy .
Insert/edit link
Enter the destination URL
Or link to existing content

- fish-shell »
- Commands »
- string-collect - join strings into one
- Introduction
- Frequently asked questions
- Interactive use
- The fish language
- Fish for bash users
- Writing your own completions
- Release notes
Quick search
- Description
string-collect - join strings into one ¶
Description ¶.
string collect collects its input into a single output argument, without splitting the output when used in a command substitution. This is useful when trying to collect multiline output from another command into a variable. Exit status: 0 if any output argument is non-empty, or 1 otherwise.
A command like echo (cmd | string collect) is mostly equivalent to a quoted command substitution ( echo "$(cmd)" ). The main difference is that the former evaluates to zero or one elements whereas the quoted command substitution always evaluates to one element due to string interpolation.
If invoked with multiple arguments instead of input, string collect preserves each argument separately, where the number of output arguments is equal to the number of arguments given to string collect .
Any trailing newlines on the input are trimmed, just as with "$(cmd)" substitution. Use --no-trim-newlines to disable this behavior, which may be useful when running a command such as set contents (cat filename | string collect -N) .
With --allow-empty , string collect always prints one (empty) argument. This can be used to prevent an argument from disappearing.
Bash variables and command substitution
Using variables to refer to data, including the results of a command.
An essential feature of programming is the ability to use a name or a label to refer to some other quantity: such as a value, or a command. This is commonly referred to as variables .
Variables can be used, at the very least, to make code more readable for humans:
However, variables really come into use in more advanced programming, when we're in a situation in which the actual values aren't known before executing a program. A variable acts as a placeholder that gets resolved upon actual execution time.
For example, imagine that websites.txt contains a list of website addresses. The following routine reads each line (via cat , which isn't best practice…but will do for now) into a for loop, which then downloads each URL:
Basic variable usage and syntax
Setting a variable.
The following command assigns Hello World to the variable named var_a , and 42 to another_var
Unlike most modern languages, Bash is pretty picky about the syntax for setting variables. In particular, no whitespace is allowed between the variable name, the equals sign, and the value.
All of these examples would cause Bash to throw an error:
Referencing the value of a variable
Whenever Bash encounters a dollar-sign , immediately followed by a word, within a command or in a double-quoted string, it will attempt to replace that token with the value of the named variable. This is sometimes referred to as expanding the variable , or parameter substitution :
Failure to dereference
When a dollar-sign doesn't precede a variable name, or a variable reference is within single-quotes , Bash will interpret the string literally :
Concatenating strings
Variables can be very useful for text-patterns that will be repeatedly used:
If your variable name butts up against a literal alphanumeric character, you can use this more verbose form, involving curly braces , to reference a variable's value:
Valid variable names
Variable names can contain a sequence of alphanumeric characters and underscores. For variables created by you, the user, they should start with either an alphabetical letter or an underscore (i.e. not a number):
Valid variable names:
- GRATUITOUSLY_LONG_NAME
When we write functions and shell scripts, in which arguments are passed in to be processed, the arguments will be passed int numerically-named variables, e.g. $1 , $2 , $3
For example:
Inside my_script.sh , commands will use $1 to refer to Hello , $2 to 42 , and $3 for World
The variable reference, $0 , will expand to the current script's name, e.g. my_script.sh
Command substitution
The standard output of a command can be encapsulated, much like a value can be stored in a value, and then expanded by the shell.
This is known as command substitution . From the Bash documentation :
Command substitution allows the output of a command to replace the command itself. Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
As an example, consider the seq command, which will print a sequence of numbers starting from the first argument to the second argument:
With command substitution, we can encapsulate the result of seq 1 5 into a variable by enclosing the command with $( and ) , and pass it as an argument to another command:
Variables and command expansion
When a command is replaced by its standard output, that output, presumably just text, can be assigned to a variable like any other value:
The loss of newlines in command substitution
Earlier, I quoted from the Bash documentation on command expansion. Here's an emphasized version of the excerpt:
What does that mean? Consider seq 1 5 being called normally, and then, via command substitution, and note the change in formatting:
Why do the newlines get removed during the command expansion? It's something we'll encounter later (and there's a section on it at the end of this tutorial) and deals with how Bash interprets space and newline characters during expansion. Anyway, it's worth noting the behavior for now, as it may be new to you if you're coming from another programming language.
Arithmetic expansion
To do basic calculations, you can enclose an expression inside $(( )) :
Check the Bash documentation for the full set of arithmetic operators . Math at the command-line can be a bit clunky so we won't be focusing too much on it.
The bc utility
An aside: if you want to do more advanced math from the command line, use bc , which reads in from stdout and evaluates the expression:
Word-splitting in the wild
This section covers more technical details of how Bash handles space characters when it does an exapansion. It's not necessary to memorize for the specific assignments in this class. However, as many of you are wont to copy and paste code directly from things you've seen on the Internet, it might be worth knowing all the different ways you could accidentally harm yourself, due to the way Bash handles spaces and newline characters.
Here's the Bash documentation for the concept known as "word-splitting"
The internal field separator
The global variable IFS is what Bash uses to split a string of expanded into separate words …think of it as how Excel knows to split a CSV (comma-separated-values) text file into a spreadsheet: it assumes the commas separate the columns.
Let's pretend that IFS has been set to something arbitrary, like a capital Z . When Bash expands a variable that happens to contain a Z , the value of that variable will be split into separate words (and the literal Z will disappear):
By default, the IFS variable is set to three characters: newline , space , and the tab . If you echo $IFS , you won't see anything because those characters…well, how do you see a space character if there aren't any visible characters?
The upshot is that you may see code snippets online in which the IFS variable is changed to something like $'\n' (which stands for the newline character).
Imagine a textfile that contains a bunch of lines of text that, for example, may refer to filenames:
When Bash reads each line of the file, the default value of IFS , which includes a space character , will cause Bash to treat the file named rough draft.txt as two files, rough and draft.txt , because the space character is used to split words.
With IFS set to just the newline character, rough draft.txt is treated as a single filename.
This concept will make sense when it comes to reading text files and operating on each line. I don't expect you to fully understand this, but only to be aware of it, just in case you are haphazardly copy-pasting code from the Internet.
The dangers of unquoted variables
In an ideal world, everyone would keep their string values short and without space/newline, or any other special characters. In that ideal world, the following unquoted variable reference would work just fine:
But when people start adding special characters to filenames, such as spaces, expanding variables, without the use of double quotes , can be dangerous .
In the following example, the programmer intends the file named Junk Final.docx to be deleted:
Unexpected word-splitting
However, when referenced without double-quotes , Bash sees file_to_kill as containing two separate values, Junk and Final.docx . The subsequent rm command will attempt to delete those two files , and not Junk Final.docx :
Unexpected special characters in filenames
Ah, no harm done , you say, because those files didn't exist in the first place. OK, but what happens when someone puts a star (i.e. asterisk ) into a filename? You're aware of what happens when you do grep * and rm * – the star acts as a wildcard, grabbing every file.
So you'll see the previous errors, since Junk and Final.docx don't exist. But in between those attempted deletions, rm will run on * …so say bye-bye to every file in that directory.
Here's the animated GIF version:
Notice how rm "$filename" affects only the file that is named, * LOL BYE FILES .
So the main takeaway here is: double-quote your variable references whenever possible .
To reiterate
Expanding a variable can lead to unexpected and sometimes catastrophic results if the variable contains special characters:
Expanding a variable within double-quotes can prevent such problems:
Who would do such a thing?
You might think, Who the hell puts star characters in their filenames? Well, besides people who really enjoy star-shaped symbols, malicious hackers and pranksters. And variables usually aren't just manually assigned by the result of human typing. As you've read above, sometimes the result of commands are stored in a variable. And if such commands are processing raw data, it's not unimaginable that the raw data, quite innocently, contains special characters that are destructive to certain Bash programs.
For the purposes of the CompCiv course, the assignments will try to stay far from untrusted sources of data. But keep in mind the dangers of just pasting in seemingly safe-looking code. Bash's syntax and behavior in handling strings is hard to fully comprehend, which is why developers use other languages for more complex applications.
You can read more about quoting variables . There's a lot of minutiae, but the main takeaway, besides general safety , is to have a general understanding how Bash, and any other programming environment, uses certain conventions and syntax rules to deal with the myriad ways that users want to pass around values in their programs.

IMAGES
VIDEO
COMMENTS
New lines are very much there in the variable "$final_list" . echo it like this with double quotes: echo "$final_list" url1 url2 url3.
If you want to store the string in a variable, use the cat command in a command substitution. The newline character(s) at the end of the string will be stripped
Basic concatenation of two strings with no separator · Join strings with special character as separator · Concatenate in a loop (append strings to a variable)
append newline to existing variables ... firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${
Inserting a Newline in a Variable in Bash · $ text="first \nsecond \nthird" $ echo $text first \nsecond \nthird · $ text="first \nsecond \nthird"
You can use newline(\n) as a separator to concatenate string variables in bash. The following script will concatenate two string variables by
$0 # bash · You may notice that newline characters are escaped in strings: ; "hello\nworld" # hello\nworld · Solution§. echo§. To preserve the
$'\n' is bash idiomatic way to represent a newline character. printf then just prints chars a to z, and the newline character.
Concatenation operation in bash is the process of attaching a string to the end of another string. Bash allows its users to concatenate strings
Any trailing newlines on the input are trimmed, just as with "$(cmd)" substitution. Use --no-trim-newlines to disable this behavior, which may be useful when
By default, the IFS variable is set to three characters: newline, space, and the tab. If you echo $IFS , you won't see anything because those characters…well