Linux array from string

Содержание
  1. Bash split string into array using 4 simple methods
  2. Sample script with variable containing strings
  3. Method 1: Bash split string into array using parenthesis
  4. Method 2: Bash split string into array using read
  5. Method 3: Bash split string into array using delimiter
  6. Method 4: Bash split string into array using tr
  7. Some more examples to convert variable into array in bash
  8. Related Posts
  9. 4 thoughts on “Bash split string into array using 4 simple methods”
  10. How to Split String in Bash Script
  11. Method 1: Split string using read command in Bash
  12. Method 2: Split string using tr command in Bash
  13. Convert a text string in bash to array
  14. 3 Answers 3
  15. Bash Array Explained: A Complete Guide
  16. Bash Indexed Array of Strings
  17. How Do You Determine the Length of a Bash Array?
  18. Access the Last Element of a Bash Array
  19. How to Print All the Values in a Bash Array
  20. How to Update a Bash Array Element
  21. Loop Through Bash Array Elements
  22. For Loop Using the Indexes of a Bash Array
  23. Using Declare For Indexed Arrays
  24. Append Elements to a Bash Indexed Array
  25. How to Remove an Element From an Array
  26. Summary of Bash Array Operations
  27. Initialize a Bash Associative Array
  28. How to Use a For Loop with a Bash Associative Array
  29. Delete an Element From an Associative Array
  30. Remove Duplicates From an Array
  31. Check If a Bash Array Contains a String
  32. Bash Array of Files in a Directory
  33. How to Reverse an Array in Bash
  34. How to Copy a Bash Indexed Array
  35. Slicing a Bash Array
  36. Search And Replace An Array Element
  37. How to Concatenate Two Bash Arrays
  38. Verify If Bash Array Is Empty
  39. Create a Bash Array From a Range of Numbers
  40. How To Implement a Push Pop Logic for Arrays
  41. Bad Array Subscript Error
  42. Conclusion

Bash split string into array using 4 simple methods

Table of Contents

How to create array from string with spaces? Or In bash split string into array? We can have a variable with strings separated by some delimiter, so how to split string into array by delimiter?

The main reason we split string into array is to iterate through the elements present in the array which is not possible in a variable.

Sample script with variable containing strings

For example in this script I have a variable myvar with some strings as element

Here if I want to iterate over individual element of the myvar variable, it is not possible because this will considered as a variable and not an array.
This we can verify by counting the number of elements in the myvar variable

When we execute the script, we see that number of elements in myvar is 1 even when we have three elements

To overcome this we convert and split string into array. Now your variable can have strings or integers or some special characters, so depending upon your requirement you can choose different methods to convert string into an array. I will cover some of them with examples:

Method 1: Bash split string into array using parenthesis

Normally to define an array we use parenthesis () , so in bash to split string into array we will re-define our variable using open and closed parenthesis

Snippet from my terminal

Method 1

Next execute the shell script. We see know we have 3 elements in the array

Method 2: Bash split string into array using read

We can use read -a where each input string is an indexed as an array variable.
the default delimiter is considered as white space so we don’t need any extra argument in this example:

Snippet from my terminal

Method 2

Execute the script. Now the myarray contains 3 elements so bash split string into array was successful

Method 3: Bash split string into array using delimiter

We can combine read with IFS (Internal Field Separator) to define a delimiter.
Assuming your variable contains strings separated by comma character instead of white space as we used in above examples
We can provide the delimiter value using IFS and create array from string with spaces

Snippet from my terminal

Method 3

Execute the shell script, and the variable is successfully converted into array and the strings can be iterated separately

Method 4: Bash split string into array using tr

tr is a multi purpose tool. We will use this tool to convert comma character into white space and further using it under parenthesis from Method 1 to create array from string with spaces
So you can use this with any other delimiter, although it may not work under all use cases so you should verify this based on your requirement

Snippet from my terminal

Method 4

Execute the shell script

Some more examples to convert variable into array in bash

Based on your requirement you can choose the preferred method.

For example here I have a variable with newline as delimiter. So here I can use the first method. This will create array from string with spaces

Execute the script. You can verify using the number of elements in the array

We can now iterate through the elements which are part of the array in a loop

Execute the script to verify

Lastly I hope the steps from the article for bash split string into array on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

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!!

4 thoughts on “Bash split string into array using 4 simple methods”

Tks. Great. Can we use the array element “$” for regex search in grep/sed/awk. If so, some examples pl.

Can you please give an example so I can help you

I found Method 3 did not work for me… Here is the slightly modifed script:

(Modified to show the bash version in use and the contents of the first array entry…)

Here is what I get:

It is possibly because my bash version is prehistoric, but I remain surprised that I get no error messages. Interesting the commas have gone when it lists My array[0] despite IFS initialisation…

Источник

How to Split String in Bash Script

Let’s say you have a long string with several words separated by a comma or underscore. You want to split this string and extract the individual words.

You can split strings in bash using the Internal Field Separator (IFS) and read command or you can use the tr command. Let me show you how to do that with examples.

Method 1: Split string using read command in Bash

Here’s my sample script for splitting the string using read command:

The part that split the string is here:

Let me explain it to you. IFS determines the delimiter on which you want to split the string. In my case, it’s a semi colon. It could be anything you want like space, tab, comma or even a letter.

Читайте также:  Код ошибки 805а0190 windows phone что это

The IFS in the read command splits the input at the delimiter. The read command reads the raw input (option -r) thus interprets the backslashes literally instead of treating them as escape character. The option -a with read command stores the word read into an array in bash.

In simpler words, the long string is split into several words separated by the delimiter and these words are stored in an array.

Now you can access the array to get any word you desire or use the for loop in bash to print all the words one by one as I have done in the above script.

Here’s the output of the above script:

Method 2: Split string using tr command in Bash

This is the bash split string example using tr (translate) command:

This example is pretty much the same as the previous one. Instead of the read command, the tr command is used to split the string on the delimiter.

The problem with this approach is that the array element are divided on ‘space delimiter’. Because of that, elements like ‘Linux Mint’ will be treated as two words.

Here’s the output of the above script:

That’s the reason why I prefer the first method to split string in bash.

I hope this quick bash tutorial helped you in splitting the string. In a related post, you may also want to read about string comparison in bash.

And if you are absolutely new to Bash, read our Bash beginner tutorial series.

Источник

Convert a text string in bash to array

How do i convert a string like this in BASH to an array in bash!

I have a string str which contains «title1 title2 title3 title4 title5» (space seperated titles)

I want the str to modified to an array which will store each title in each index.

3 Answers 3

In order to convert the string to an array, say:

The shell would perform word splitting on spaces unless you quote the string.

In order to loop over the elements in the thus created array:

Another method using read:

I am in an unfortunate position where I have a long-standing shell script that is upgrading from internally using scalar variables to using arrays. But, users are also allowed to set those variables themselves, optionally, with a file that is sourced on startup.

So, I needed to make a way to conditionally convert a scalar to an array, depending on whether the user’s sourced script is declared in the new way or the old way.

My solution is overkill for the simple case above, but I wanted to document it for others looking for this solution, because it was non-trivial coming up with something that handled my use case.

The only way I’ve come up with to reliably handle all options safely is thus:

It cleanly handles all cases: existing variable, existing array, and not set at all.

Also note that while this may seem like overkill:

. it handles the case that most people are building their sourced configurations something like this:

In these cases, the $ADDITIONAL_OPTIONS variable will start with a space. If you know your inputs won’t need trimming, that bit is unnecessary.

Источник

Bash Array Explained: A Complete Guide

The Bash array data type gives you a lot of flexibility in the creation of your shell scripts.

Bash provides two types of arrays: indexed arrays and associative arrays. Indexed arrays are standard arrays in which every element is identified by a numeric index. In associative arrays every element is a key-value pair (similarly to dictionaries in other programming languages).

In this tutorial we will start by getting familiar with indexed arrays, then we will also see how associative arrays differ from them (they also have few things in common).

By the end of this tutorial you will clarify any doubts you might have right now about arrays in Bash.

And you will also learn lots of cool things you can do in your shell scripts with arrays.

Let’s get started!

Bash Indexed Array of Strings

We will start by creating an indexed array of strings where the strings are directory names in a Linux system:

First of all let’s see what gets printed when we echo the value of the array variable dirs:

When you print a Bash array variable the result is the first element of the array.

Another way to print the first element of the array is by accessing the array based on its index.

Bash indexed arrays are zero based, this means that to access the first element we have to use the index zero.

Wonder why we are using curly brackets?

We can understand why by removing them to see what the ouput is:

Bash prints the first element of the array followed by [0] because it only recognises $dirs as a variable. To include [0] as part of the variable name we have to use curly brackets.

In the same way, to print the second element of the array we will access the index 1 of the array:

What if we want to access the last element of the array?

Before doing that we have to find out how to get the length of a Bash array…

How Do You Determine the Length of a Bash Array?

To find the length of an array in Bash we have to use the syntax $<#array_name[@]>.

Let’s apply it to our example:

The syntax might seem hard to remember when you see it for the first time…

…but don’t worry, just practice it a few times and you will remember it.

Access the Last Element of a Bash Array

Now that we know how to get the number of elements in a Bash array, we can use this information to retrieve the value of the last element.

First we have to calculate the index of the last element that is equal to the number of elements in the array minus one (remember that Bash arrays are zero-based as usually happens in most programming languages).

This value will be the index to pass when we want to print the last element of the array:

Definitely not one of the easiest ways to retrieve the last element of an array, if you are familiar with other programming languages 😀

Since Bash 4.2 arrays also accept negative indexes that allow to access elements starting from the end of the array.

To verify your version of Bash use the following command:

To access the last element of a Bash indexed array you can use the index -1 (for Bash 4.2 or later). Otherwise use the following expression $-1]>.

As expected we get back the last element.

How to Print All the Values in a Bash Array

To print all the elements of an array we still need to use the square brackets and replace the index with the @ symbol:

An alternative to the @ is the * sign:

Why two ways to do the same thing?

What’s the difference between * and @ when used to print all the elements of a Bash array?

Читайте также:  Классификация защиты семейства ос windows

We will see it later, after showing you how to use a for loop to go through all the elements of an array…

How to Update a Bash Array Element

Now, how can we update an element in our array?

We will use the following syntax:

In our case I want to set the value of the second element (index equal to 1) to “/usr”.

Loop Through Bash Array Elements

Let’s find out how to create a for loop that goes through all the elements of an array:

Going back to the difference between * and @, what happens if we replace $ with $ ?

The difference becomes evident when we surround the two expressions with double quotes.

Using @

Using *

You can see that when we use the * our array is interpreted as a single value.

For Loop Using the Indexes of a Bash Array

Let’s try something else…

We will use the following expression:

Notice that we have added an exclamation mark before the name of the array.

Let’s see what happens when we do that.

This time instead of printing all the elements of the array we have printed all the indexes.

The expression $ is used to print all the indexes of a Bash array.

As you can imagine we can use this to create a for loop that instead of going through all the elements of the array goes through all the indexes of the array. From 0 to the length of the array minus 1:

Verify that the output is identical to the one we have seen by going through all the elements in the array instead of all the indexes.

We can also print the index for each element if we need it:

Using Declare For Indexed Arrays

We have created our indexed array in the following way:

Below you can see other two ways to create indexed arrays:

Option 1

Define an empty array and set its elements one by one:

Option 2

Using the Bash declare builtin with the -a flag:

Append Elements to a Bash Indexed Array

To append an element to an existing array we can use the following syntax:

What about appending more than one element?

Here’s how you can do it…

How to Remove an Element From an Array

To remove an element from an array you can use unset:

Notice how the third element of the array (identified by the index 2) has been removed from the array.

You can also use unset to delete the entire array:

Confirm that the last echo command doesn’t return any output.

Summary of Bash Array Operations

Before moving to associative arrays I want to give you a summary of the Bash array operations we have covered.

Syntax Description
array=() Create an empty array
declare -a array Create an empty indexed array with declare
array=(1 2 3 4 5) Initialise an array with five elements
$ Access first element of the array
$ Access second element of the array
$-1]> Access last element of the array
$ Get all the elements of the array
$ Get all the indexes of the array
array+=(6 7) Append two values to the array
array[2]=10 Assign value to the third element of the array
$<#array[@]> Get size of the array
$<#array[n]> Get the length of the nth element

Practice all the commands in this table before continuing with this tutorial.

Many of the operations in the table also apply to associative arrays.

Initialize a Bash Associative Array

Associative arrays can be only defined using the declare command.

As we have seen before, to create an indexed array you can also use the following syntax:

To create an associative array change the flag passed to the declare command, use the -A flag:

Notice how the order of the elements is not respected with Bash associative arrays as opposed as with indexed arrays.

When you have an array with lots of elements it can also help writing the commands that assign key/value pairs to the array in the following way:

How to Use a For Loop with a Bash Associative Array

The syntax of the for loops for associative arrays is pretty much identical to what we have seen with indexed arrays.

We will use the exclamation mark to get the keys of the array and then print each value mapped to a key:

Can you see how every key is used to retrieve the associated value?

Delete an Element From an Associative Array

Let’s see how you can delete an element from an associative array…

The following command removes the element identified by the key key1 from the associative array we have defined previously.

Confirm that you get the following output when you execute the for loop we have seen in the previous section:

To delete the full array you can use the same syntax we have seen with indexed arrays:

The next few sections will show you some useful operations you can perform with Bash arrays as part of your day-to-day scripting…

Remove Duplicates From an Array

Have you ever wondered how to remove duplicates from an array?

To do that we could use a for loop that builds a new array that only contains unique values.

But instead, I want to find a more concise solution.

We will use four Linux commands following the steps below:

  1. Print all the elements of the array using echo.
  2. Use tr to replace spaces with newlines. This prints all the element on individual lines.
  3. Send the output of the previous step to the sort and uniq commands using pipes.
  4. Build a new array from the output of the command created so far using command substitution.

This is the original array and the output described up to step 3:

Now, let’s use command substitution, as explained in step 4, to assign this output to a new array. We will call the new array unique_numbers:

The following for loops prints all the elements of the new array:

The output is correct!

I wonder if it also works for an array of strings…

Notice how we have written the Bash for loop in a single line.

Here is the output. It works for an array of strings too…

With this example we have also seen how to sort an array.

Check If a Bash Array Contains a String

To verify if an array contains a specific string we can use echo and tr in the same way we have done in the previous section.

Then we send the output to the grep command to confirm if any of the elements in the array matches the string we are looking for.

Here is how it works if, for example, we look for the string “command”:

We can use the -q flag for grep to avoid printing any output. The only thing we need is the exit code of the command stored in the $? variable.

We can then use an if else statement to verify the value of $?

This is how we verify if the array has an element equal to “command”.

In the same way we can find if a Bash associative array has a key.

We would simply replace $ with $ to print all the keys instead of the values.

Bash Array of Files in a Directory

I want to show you another example on how to generate an array from a command output.

This is something you will definitely find useful when creating your scripts.

We will create an array from the output of the ls command executed in the current directory:

Once again, notice how we use command substitution to assign the output of the command to the elements of the array.

How to Reverse an Array in Bash

We can use a very similar command to the one used to remove duplicates from an array also to reverse an array.

The only difference is that we would also use the Linux tac command (opposite as cat) to reverse the lines we obtain from the elements of the array:

How to Copy a Bash Indexed Array

Here is how you can copy an indexed array in Bash.

Given the following array:

I can create a copy using the following command:

With a for loop we can confirm the elements inside the copy of the array:

Slicing a Bash Array

Sometimes you might want to just get a slice of an array.

A slice is basically a certain number of elements starting at a specific index.

This is the generic syntax you would use:

Let’s test this expression on the following array:

Two elements starting from index 1

One element starting from index 0

Three elements starting from index 0

To get all the elements of an array starting from a specific index (in this case index 1) you can use the following:

Search And Replace An Array Element

At some point you might need to search a replace an element with a specific value…

…here’s how you can do it:

In our array I want to replace the word bash with the word linux:

I wonder if it works if there are multiple occurrences of the element we want to replace…

How to Concatenate Two Bash Arrays

I want to concatenate the following two arrays:

I can create a new array as a result of a merge of the two arrays:

Let’s confirm the values and the number of elements in this array:

Verify If Bash Array Is Empty

Why would you check if a Bash array is empty?

There are multiple scenarios in which this could be useful, one example is if you use an array to store all the errors detected in your script.

At the end of your script you check the number of elements in this array and print an error message or not depending on that.

We will use an array called errors and a Bash if else statement that checks the number of elements in the array.

In this example I will create the errors array with one element:

When I run the script I get the following output:

A nice way to track errors in your scripts!

Create a Bash Array From a Range of Numbers

How can I create an array whose elements are numbers between 1 and 100?

We will do it in the following way:

  • Create an empty array.
  • Use a for loop to append the numbers between 1 and 100 to the array.

Give it a try and confirm that the numbers between 1 and 100 are printed by the script.

We can also verify the number of elements in the array:

How To Implement a Push Pop Logic for Arrays

Given an indexed array of strings:

I want to implement a push pop logic…

…where push adds an element to the end of the array and pop removes the last element from the array.

Let’s start with push, we will just have to append an element the way we have seen before:

The pop logic gets the value of the last element and then removes it from the array:

You can also wrap those commands in two Bash functions so you can simply call push() and pop() instead of having to duplicate the code above every time you need it.

Bad Array Subscript Error

At some point while I was working on this tutorial I encountered the following error:

I was executing the following script:

Apparently there wasn’t anything wrong in line 4 of the script.

As we have seen in one of the previous sections, the index -1 can be used to access the last element of an array.

After a bit of troubleshooting I realised that the problem was caused by…

…the version of Bash running on my machine!

In version 3 Bash didn’t support negative indexes for arrays and, as explained in the section of this article “Access the Last Element of a Bash Array”, alternative solutions are possible.

Another option is upgrading your version of Bash as long as it’s supported by your operating system.

Let’s see another scenario in which this error can occur…

Here’s another script:

As you can see I’m using the declare builtin to create an associative array (I’m using the -A flag as learned in one of the sections above).

When I run the script I see the following error:

This time, as you can see from the message, the error is caused by the fact that I’m trying to add an element with an empty key to the array.

This is another reason why this error can occur.

So, now you know two different causes of the “bad array subscript” error and if you see it in your scripts you have a way to understand it.

Conclusion

We have covered so much in this blog post!

You should be a lot more comfortable with Bash arrays now compared to when you started reading this article.

Let’s do a quick recap of the topics we covered, you might spot something you want to go back and review.

We have seen how to:

  • Define indexed and associative arrays.
  • Determine the length of an array.
  • Access elements based on indexes (for indexed arrays) and keys (for associative arrays).
  • Print all the elements using either @ or *.
  • Update array elements.
  • Loop through a Bash array using either the elements or the indexes.
  • Create indexed and associative arrays using the declarebuiltin.
  • Append elements to an existing array.
  • Removeelements from an array or delete the entire array.
  • Remove duplicates from an array.
  • Check if an array contains an element that matches a specific string.
  • Reverse, copy and get a slice of an array.
  • Search and replace a string in arrays.
  • Concatenate two arrays and verify if an array is empty.
  • Create an array from a range of numbers.
  • Implement a push / pop logic for Bash arrays.
  • Understand the “bad array subscript” error.

Now it’s your time to use Bash arrays….

…but before finishing I have a question for you to test your knowledge.

Given the following array:

  1. What type of array is this one? Indexed or associative?
  2. How can you print the keys of this array?

Источник

Читайте также:  Нужен ли оптимизатор для windows
Оцените статью