4

Example:

bash script.sh "hello world"

(in script echo "$1")

hello world

Question:

bash script.sh "good" "morning" "everybody"

What do I have to write in my script to output directly:

goodmorningeverybody

So, in general, I want $1, $2, $3, ... (can be 100 but I don't know) to be saved in one variable for example VAR1.

2
  • Do you really want all the spaces gone? What are you planning to do with the variable once you have it? Commented Dec 5, 2014 at 13:54
  • @EtanReisner Yes, The script does something like this script.sh '12' '+' "23" "*" "33" and I want to calculate it, so the output must be "12+23*33" and then command bc Commented Dec 22, 2014 at 11:05

4 Answers 4

5

You can refer to all the positional arguments with $* and $@.

From 3.4.2 Special Parameters

*

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c…", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

@

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" …. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

So to get good morning everybody as output you could just use echo "$*" or echo "$@".

In general @ is the more useful of the two variables.

However, if you really want the worlds all smushed together the way you indicate then you have a few options.

The most straightforward of which is a simple loop:

for word; do
    s+=$word
done

(for without the in <list> part operates on the positional arguments).

However, you can also do this with * by controlling IFS.

So you could also do s=$(IFS=; echo "$*"). You want/need the sub-shell to avoid setting IFS for the current shell.

Sign up to request clarification or add additional context in comments.

3 Comments

Using IFS like this is a good idea, but a subshell is a very expensive way to do simple string manipulation. An alternative is to localize IFS in a function. For example (all on one line because it's in a Stackoverflow comment): function concat { local IFS=; concat_return="$*"; } ; concat good morning everybody ; echo "$concat_return".
@pjh While true that's only "better" if you don't mind the global variable/side-effect aspect of that function usage but yes.
@EtanReisner, I agree that using a variable to return the concatenated string is not necessarily "better" than the alternative that uses a subshell. The dangers associated with the variable can be hugely reduced by localizing it in the caller, before calling concat. There is no perfect solution because Bash has no (generally) good way to return non-trivial values from functions.
3

Try doing this:

#!/bin/sh

var=$(printf '%s' "$@")
echo "$var"

or even better, credits to chepner :

printf -v var '%s' "$@"

or

#!/bin/bash

for arg; do
    str+="$arg"
done

echo "$str"

Output :

goodmorningeverybody

Note :

"$@" expands to each positional parameter as its own argument: "$1" "$2" "$3"...

1 Comment

In bash, you can save a fork using printf -v var '%s' "$@".
1

You could do it with a loop:

for x in "$@"; do
  input="$input$x"
done

Comments

1

You can do it using Shell-Parameter-Expansion in this way:

#!/bin/bash
VAR="$*"
VAR=${VAR// /}
echo $VAR

Example

$ script.sh "good" "morning" "everybody"
goodmorningeverybody

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.