1. Introduction

Joining in Bash means merging a number of arguments into a single string. This is commonly done to pass them as a unified parameter. Additionally, it simplifies handling a variable number of arguments and ensures proper formatting for interacting with commands or functions. Hence, this is useful when dealing with commands that expect a single parameter, or when constructing complex commands dynamically.

In this tutorial, we’ll discuss different methods used to join Bash arguments into a single string separated by space.

First, we’ll explore the use of single quotes for joining Bash arguments into a single string. Following that, we’ll look into using IFS (Internal Field Separator) to achieve similar outcomes. Subsequently, we’ll examine positional arguments as an alternative method for joining Bash arguments. Lastly, we’ll utilize the paste command to accomplish similar results.

2. Using Single Quotes

To begin with, we can use single quotes to join Bash arguments into a single string separated by space.

Let’s take a look at an example script:

$ cat paraExp.sh
#!/bin/bash
str="'$*'"
echo "$str"

Now, we make the file executable using the chmod command, and run the script:

$ chmod +x paraExp.sh
$./paraExp.sh one two three four 5 6 'seven with spaces'
'one two three four 5 6 seven with spaces'

The above script takes all command-line arguments and concatenates them into a single string called str, enclosed in single quotes. Let’s understand the script by breaking it down:

  • ‘$*’ represents all the command-line arguments as a single string
  • str=”‘$*'” assigns this string to the variable str, enclosed in single quotes

Lastly, we print the output string using the echo command.

Therefore, when the script is run with the specified command-line arguments one two three four 5 6, the output is ‘one two three four 5 6’. The entire string is enclosed in single quotes as specified in the script. Crucially, $* concatenates all the arguments separated by the default IFS, which is a space.

3. Using IFS

Similarly, we can use IFS for joining space-separated arguments into a single string.

IFS in Bash is crucial for controlling field separation when handling strings. Hence, it’s used in parsing file values, managing user input, and iterating through array elements.

To see how IFS affects our case, we use $* with IFS.

Now, let’s take a look at the script:

$ cat IFS_eg1.sh
#!/bin/bash
oldIFS="$IFS"
IFS=':'
arguments="$*"
echo "Arguments using \$* and IFS: $arguments"
IFS="$oldIFS"

Next, we make the script executable and run it:

$ chmod +x IFS_eg1.sh
$./IFS_eg1.sh arg1 arg2 "'arg3 with spaces and single quotes'"
Arguments using $* and IFS: arg1:arg2:'arg3 with spaces and single quotes'

In the above script, we again use the $* to concatenate all command-line arguments into a single string. This approach is similar to the one we discussed in the previous section. However, we modify the default field separator in this example.

First, we assign the current IFS value to the oldIFS variable. Subsequently, we update IFS by setting it to colon. The string is then formed using the construct str=”‘$*'”.

Moreover, we pass the argument enclosed in single quotes to visualize the effect of IFS on that string. As per the script, the arguments are concatenated into a single string using the modified IFS, while preserving the white spaces within the quoted argument.

Finally, we print the combined string and restore the original value of IFS.

4. Using Positional Arguments

Alternatively, we can also use the positional argument to concatenate Bash arguments into a single string with space separators.

Positional arguments, values, or variables passed to scripts follow a defined order, enabling flexible customization and automation. Thus, they facilitate parameter passing and frequently play a crucial role in command-line interfaces.

Now, let’s view the poArg.sh script:

$ cat poArg.sh
#!/bin/bash
array="${@}"
echo "Joined arguments: $array"

Next, we execute and check the output of the script:

$ chmod +x poArg.sh
$ ./poArg.sh apple banana fruit "basket" 3 grapes
Joined arguments: apple banana fruit basket 3 grapes

This script takes all command-line arguments and concatenates them into a single string variable named array. After that, it prints the joined arguments to the terminal. In particular, we use array=”${@}” to capture all command-line arguments into the variable array, ensuring the proper handling of spaces with “${@}” in double quotes.

Thus, in the above example, the script joins and displays all provided command-line arguments in a single string, showcasing proper handling of spaces within the arguments.

Similarly, we can replace @ with an asterisk * in the positional argument to get similar results:

$ cat poArg2.sh
#!/bin/bash
array="${*}"
echo "Joined arguments: $array"

Now, let’s run the script:

$ chmod +x poArg2.sh
$./poArg2.sh arg1 arg2 arg3 "with spaces" 4 5
Joined arguments: arg1 arg2 arg3 with spaces 4 5

Likewise, this Bash script also captures and concatenates command-line arguments into a variable named array using “${*}”, ensuring the proper handling of spaces.

5. Handling Arguments as Separate Entities With IFS

In contrast, $@ represents each command-line argument as a separate entity.

Let’s take a look at an example script:

$ cat IFS_eg2.sh
#!/bin/bash
oldIFS="$IFS"
IFS=':'
arg="$@"
echo "Argument using \$@: $arg"
oldIFS="$IFS"

Next, we execute and check the output of the script:

$ chmod +x IFS_eg2.sh
$./IFS_eg2.sh arg1 arg2 "'arg3 with spaces and single quotes'"
Argument using $@ and IFS: arg1 arg2 'arg3 with spaces and single quotes'

In the above script, we use $@ to concatenate the command-line arguments into a single string. Notably, the IFS change doesn’t affect the concatenation of command-line arguments in this case. The reason is that when assigning arg=”$@”, the arguments are concatenated using the default IFS, which is space, regardless of the change we made to IFS earlier in the script.

6. Handling Arguments Using ${array[*]}

To join command-line arguments with a modified IFS, a common approach uses arg=”$@”.

However, due to the presence of double quotes, this method treats all arguments as a single word. Thus, there’s no impact of the modified IFS.

To achieve the desired result, we assign the command-line arguments as an array to a Bash variable, and then concatenate the array elements into a single string:

$ cat IFS_eg3.sh
#!/bin/bash
oldIFS="$IFS"
IFS=':'
args=("$@")
new_arg="${args[*]}"
echo "Argument using \$@: $new_arg"
IFS="$oldIFS"

Once we execute the script, we see the combined string with the modified IFS:

$ chmod +x IFS_eg3.sh
$./IFS_eg2.sh arg1 arg2 'arg3 with spaces and single quotes'
Argument using $@: arg1:arg2:arg3 with spaces and single quotes

Let’s look at the changes we made:

  • args=(“$@”) initializes an array args with command-line arguments
  • “${args[*]}” concatenates the array elements into a single string

Hence, this approach ensures that the script considers the modified IFS during the concatenation process.

7. Using the printf Command

An alternative approach is to use the printf command when we want to compile a single string with space-separated arguments.

Let’s view the printf_join.sh script to check how we can join the arguments:

$ cat printf_join.sh
#!/bin/bash
result=$(printf '%s/' "$@")
echo "Output: $result"

Now, we can run the script and visualize its output:

$ chmod +x printf_join.sh
$ ./printf_join.sh arg1 arg2 "arg3 with spaces" 4 5
Output: arg1/arg2/arg3 with spaces and single quotes/4/5

This Bash script uses printf to join command-line arguments into a single string with elements separated by /. Specifically, printf ‘%s/’ “$@” formats and prints the command-line arguments with / following each one. Importantly, the reason that this syntax works is the way printf handles $@.

Thus, when we run this script, the output joins the Bash arguments into a single string separated by forward slashes /.

8. Conclusion

In this article, we discussed different methods used to join Bash arguments separated by space into a single string.

Initially, we explored using single quotes for joining Bash arguments into a single string. Subsequently, we examined positional arguments as an alternative method for joining Bash arguments.  Following that, we looked into using IFS to achieve similar outcomes. In addition, we discussed the impact of IFS on $* and $@.

Finally, we utilized the paste command to accomplish similar results.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments