Reputation: 6494
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"'
Upvotes: 3
Views: 143
Reputation: 74028
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
Upvotes: 3
Reputation: 6568
Try this
#!/bin/bash
params=("$@")
for i in "${params[@]}"
do
string="${string} \'$i\'"
done;
// subst with your full command, this is only for test purposes
sh -c "echo ${string}"
Testing:
[root@myhost test]# sh ./script.sh aaaa "bbbb cccc"
'aaaa' 'bbbb cccc'
Upvotes: 2
Reputation: 4648
Try:
gnome-terminal -x sh -c `echo "$@"`
although I'm not sure I understand what the script is ultimately trying to do (it appears you want to execute one or more files which may contain spaces in their file names).
Upvotes: 0