I'm writing a bash script where I'm reading in a variable via read this two strings:

log.*.console.log log.*.log

They are separated by space.

How can I rewrite the strings that the output from the variable for the next program called in the script will have them in this form ? 'log.*.console.log' 'log.*.log'

I was trying sed and awk but somehow without success.

The whole script:

if [ -z "$*" ] ; then
echo "USAGE: ./some text"
exit 1


echo "some text"
read varlogname

for file in $@
echo "Doing" $file
GTAR=$(gtar -xvf $file --wildcards --ignore-case --no-anchored "$varlogname")

for file in ${GTAR[*]}
mv $file $file"."${i}
echo "Files extracted."
exit 0
  • 2
    and not sure why you need to add the quotes... perhaps going through this might solve your issue: mywiki.wooledge.org/Quotes – Sundeep Apr 10 '17 at 8:19
  • @Sundeep I'm having this in my script GTAR=$(gtar -xvf $file --wildcards --ignore-case --no-anchored "$varlogname") inside a loop. where the $varlogname is the variable which I'm reading in via read, the problem is gtar needs this as a separator '' between files. But since it's a loop it has to be inside "" within the script. – DaWe4444 Apr 10 '17 at 8:27
  • @Sundeep added the whole script into original post, so it's better to read. – DaWe4444 Apr 10 '17 at 8:30
  • 2
    check you script with shellcheck.net correct the errors and update this question – Sundeep Apr 10 '17 at 8:40

I don't think you want to give the single quotes to gtar. In a command such as somecmd 'foo bar' 'files*.log', the shell will handle the quotes, they tell it not to treat special characters specially, and pass somecmd the arguments foo bar and files*.log. Unix programs do not get the command-line as one string, but a number of argument strings, it is the shell that does the splitting of the command-line to strings.

If you want to read multiple values in Bash, you could use read -a array, and then hand the array to the command.

  -a array  assign the words read to sequential indices of the array
            variable ARRAY, starting at zero


read -a filenames
gtar "${filenames[@]}"

Indexing the array with [@] (in quotes) expands to the array members as separate words, which is what you want.

Also, you have for file in ${GTAR[*]}. That looks like accessing GTAR as an array, but it isn't one, you've just assigned the output of the gtar command to it, as a string. In this case ${GTAR[*]} is the same as $GTAR. Since the expansion isn't quoted the string undergoes word-splitting at this point, which by default splits on whitespaces. But, after that, the words are also taken as filename globs and expanded to matching filenames.

As long as your filenames don't contain whitespace or glob characters (*?[], this is fine. But in general, it's not something you want to do.

For proper arrays, you probably always want to use "${array[@]}" instead of [*].

See: Word Splitting, Why does my shell script choke on whitespace or other special characters? and Arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.