• 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?

Matt Fenwick's user avatar

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 :

anubhava's user avatar

It may depend on how you're trying to display the final result. Try outputting the resulting variable within double-quotes:

Emo Mosley's user avatar

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...)

Venkatesha K's user avatar

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 .

Hot Network Questions

bash join variables with newline

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?

Gilles 'SO- stop being evil''s user avatar

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.

enzotib's user avatar

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.

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).

pihentagy's user avatar

Are you using "echo"? Try "echo -e".

Mike's user avatar

From all discussion, here is the simplest way for me:

The echo command must use double quotes .

kholis's user avatar

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.

mklement0's user avatar

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.

Paul Tobias's user avatar

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 .

Michał Šrajer's user avatar

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 .

Hot Network Questions

bash join variables with newline

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.

Buy GoLinuxCloud a Coffee

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.

Member Information Avatar

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]

bash join variables with newline

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. 

 concatenate string literals

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. 

concatenate string with number

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. 

Concatenate string without any separator

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

5. Concatenating variables with a separator

Concatenate string with 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. 

Concatenate string using operator (+=)

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. 

Concatenate string with newline

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

Concatenate string with 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. 

Concatenate string using printf

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 join variables with newline

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:

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.

bash join variables with newline

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.

Bash script to join two strings

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:

Bash string concatenation of numbers and strings

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:

Connecting both strings without any built-in function or command in bash. Combined values are achieved using the append += operator

A similar result can be achieved using two variables:

Bash append string operator use-case example. Two strings are taken and concatenated string is made with the help of the append operator

Concatenating Numeric Strings

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

Bash script to join numeric string variables. Plus and equal sign correspond to append operator

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

Bash script to add three numbers together

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:

Bash for loop example in a bash script. It is used to join three strings with the preferred string. Echo command prints out the combined result

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

bash join variables with newline

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

bash join variables with newline

Quick search

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:

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.


  1. How to use Variables in Bash

    bash join variables with newline

  2. How to use Variables in Bash

    bash join variables with newline

  3. Bash Variables

    bash join variables with newline

  4. bash: 未预期的符号 `newline' 附近有语法错误_RGiant的博客-CSDN博客_未预期的符号newline附近有语法错误

    bash join variables with newline

  5. Bash Variables

    bash join variables with newline

  6. PPT

    bash join variables with newline


  1. Battle of Queens: Who Won the Dance-Off Btw Foluke Daramola & Ayo Adesanya at Faithia Balogun's

  2. 24

  3. Bash functions with parameters

  4. BASH Shell commands wildcard |Shell Scripting Tutorial in Hindi


  6. Check If A Variable Is Set/Empty In Bash


  1. Concatenating two string variables in bash appending newline

    New lines are very much there in the variable "$final_list" . echo it like this with double quotes: echo "$final_list" url1 url2 url3.

  2. How to add newlines into variables in bash script

    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

  3. Bash string concatenate possible? [SOLVED]

    Basic concatenation of two strings with no separator · Join strings with special character as separator · Concatenate in a loop (append strings to a variable)

  4. concat 2 variables with newline

    append newline to existing variables ... firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${

  5. Inserting a Newline in a Variable in Bash

    Inserting a Newline in a Variable in Bash · $ text="first \nsecond \nthird" $ echo $text first \nsecond \nthird · $ text="first \nsecond \nthird"

  6. How to Concatenate String Variables in Bash [Join Strings]

    You can use newline(\n) as a separator to concatenate string variables in bash. The following script will concatenate two string variables by

  7. Bash string with newline

    $0 # bash · You may notice that newline characters are escaped in strings: ; "hello\nworld" # hello\nworld · Solution§. echo§. To preserve the

  8. Bash One-Liners Explained, Part II: Working with strings

    $'\n' is bash idiomatic way to represent a newline character. printf then just prints chars a to z, and the newline character.

  9. How to Bash Concatenate Strings: Joining Variables 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

  10. 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

  11. Bash variables and command substitution

    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