Skip to main content
added 344 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 177
  • 165

I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all:

str="'$*'" 

$* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space.

Inside a double quoted string, there is no need to escape single-quotes.

Example

Let us put the above in a script file:

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

Now, run the script with sample arguments:

$ sh script.sh one two three four 5 'one two three four 5' 

This script is POSIX. It will work with bash but it does not require bash.

A variation: concatenating with slashes instead of spaces

We can change from spaces to another character by adjusting IFS:

$ cat script.sh #!/bin/sh old="$IFS" IFS='/' str="'$*'" echo "$str" IFS=$old 

For example:

$ sh script.sh one two three four 'one/two/three/four' 

I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all:

str="'$*'" 

$* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space.

Inside a double quoted string, there is no need to escape single-quotes.

Example

Let us put the above in a script file:

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

Now, run the script with sample arguments:

$ sh script.sh one two three four 5 'one two three four 5' 

This script is POSIX. It will work with bash but it does not require bash.

I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all:

str="'$*'" 

$* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space.

Inside a double quoted string, there is no need to escape single-quotes.

Example

Let us put the above in a script file:

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

Now, run the script with sample arguments:

$ sh script.sh one two three four 5 'one two three four 5' 

This script is POSIX. It will work with bash but it does not require bash.

A variation: concatenating with slashes instead of spaces

We can change from spaces to another character by adjusting IFS:

$ cat script.sh #!/bin/sh old="$IFS" IFS='/' str="'$*'" echo "$str" IFS=$old 

For example:

$ sh script.sh one two three four 'one/two/three/four' 
Source Link
John1024
  • 76.4k
  • 12
  • 177
  • 165

I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all:

str="'$*'" 

$* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space.

Inside a double quoted string, there is no need to escape single-quotes.

Example

Let us put the above in a script file:

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

Now, run the script with sample arguments:

$ sh script.sh one two three four 5 'one two three four 5' 

This script is POSIX. It will work with bash but it does not require bash.