Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash, how do I join N parameters together as a space separated string

I'm trying to write a function that takes n parameters and joins them into a string.

In Perl it would be

my $string = join(' ', @ARGV);

but in bash I don't know how to do it

function()
{
    ??
}
like image 938
user983124 Avatar asked Sep 05 '12 14:09

user983124


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do I pass a string to a function in bash?

Bash makes it extremely easy to define functions with parameters. In this example, we will create the hello_world function and pass a string as an argument by its position using shell variables. That is $1 , $2 , and so forth. Copy #!/bin/bash hello_world () { echo "Hello $1" } hello_world "World Again!"


1 Answers

Check the bash man page for the entry for '*' under Special Parameters.

join () {
    echo "$*"
}
like image 108
chepner Avatar answered Oct 21 '22 14:10

chepner