Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "$@" inside others quotes in bash

Here is the code of the bash script I want to execute : opening a new console and execute in it clamscan with a list of files

gnome-terminal -x sh -c "clamscan $@"

After expansion here what is really executed if the 2 arguments given to the script are
file1.txt file2.txt

gnome-terminal -x sh -c 'clamscan file1.txt' file2.txt

As you see only clamscan file1.txt is executed.

If I try another way to write the code

gnome-terminal -x sh -c "clamscan \"$@\""

Here is the expansion result

gnome-terminal -x sh -c 'clamscan "file1.txt' 'file2.txt"'

It doesn't work better.
Does anyone knows how to properly integrate "$@" inside others quotes?

Edit My goal is to have after expansion something like

gnome-terminal -x sh -c 'clamscan "file1.txt" "file2.txt"'
like image 745
Nicolas Avatar asked May 07 '26 06:05

Nicolas


1 Answers

From man bash

3.4.2 Special Parameters
@
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" ….

The important part is equivalent, it does not expand to "$1" "$2", i.e. with double quotes. If you need that, you have two choices, prepare the parameters yourself

for i in "$@"; do
    args="$args \"$i\""
done

gnome-terminal -x clamscan $args

or put part of the command line in a script

script.sh:

clamscan "$@"

and call that

gnome-terminal -x script.sh file1.txt file2.txt
like image 143
Olaf Dietsche Avatar answered May 09 '26 00:05

Olaf Dietsche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!