1

I need to concatenate 2 string variables in bash on my Centos 6 machine

for f in *.tar.gz;
do
basename ${f%.tar.gz}
echo $basename
dbaddress='mysql/*.sql'
path="${basename}${dbaddress}"  
echo $path
tar -zxvf $f $path
done

The script should extract all .sql files inside basename/mysql/ directory. But the output of my code is as follows:

basename

mysql/*.sql
tar: mysql/*.sql: Not found in archive
tar: Exiting with failure status due to previous errors

As you see, the $basename is not prepended to $dbaddress . Am I missing something?

2

You are referencing the basenamevariable (with $basename), but are never assigning it to anything, which means it reads to bash as a null string. You might need to try basename=${f%.tar.gz}.

  • Thank you, It worked like a charm! But I'm wondering why echo $basename is printing correct filename. – KarenV Nov 24 '15 at 19:22
  • It wasn't; it was echoing a blank line, because $basename was undefined. the basename command is what was outputting the filename root. See man basename- if you don't declare an extension, it returns the entire filename of the specified file (even if it does not exist). – DopeGhoti Nov 24 '15 at 19:29
1

You may try this to combine two variables:

var1="Hello"
var2="$var1 world!"

But in your code, you aren't declaring the basename variable!

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.