How to expand variable in bash tar command [duplicate] - linux

This question already has answers here:
bash script execute command with double quotes, single quotes and spaces
(2 answers)
Variable containing multiple args with quotes in Bash
(4 answers)
Closed 1 year ago.
I have a tar command to create .tar.gz file. It looks like below:
exclude="--exclude='.cache' --exclude='.composer' --exclude='.local' --exclude='.config'"
tar "$exclude" -czf "$backupsDir/$timestamp-home.tar.gz" /home
The problem is the $exclude variable in not evaluated and excluded directories are still in the archive. Can somebody tell me what is wrong with it? Thanks a lot.

Related

How do I pass quoted shell variables as arguments correctly? [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Variable containing multiple args with quotes in Bash
(4 answers)
Closed 10 months ago.
As part of a build pipeline I have a shell script that zips up a directory. I want to use a variable to define which patterns should be ignored.
I'm doing something like this:
IGNORE='"*.md" "some-folder/*"'
zip -x $IGNORE -r my-zip.zip ./*
Which doesn't appear to work, the ignored files and folders are still included in the zip archive.
This does work if I create the command and then pipe it through to sh though, so I'm confident that the variable contains the correct values:
echo "zip -x $IGNORE -r my-zip.zip ./*" | sh
I think it might be something to do with the quotes, since this works as expected without them. However this fails as soon as I attempt to add more than 1 pattern to IGNORE.
IGNORE=*.md
zip -x $IGNORE -r my-zip.zip ./*
What am I missing in order to be able to pass these patterns correctly quoted?
Edit: this does also not appear to work as an array, as suggested by this question.
IGNORE='"*.md" "some-folder/*"'
EXCLUDE=($IGNORE)
zip -x ${EXCLUDE[#]} -r my-zip.zip ./*

Batch remove extra file extensions in bash [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
bash - command substitution is omitting whitespaces
(1 answer)
Closed 12 months ago.
I converted some files to another format but in doing so added an extra extension. For example foo.bar.temp. I wrote a script to delete the .temp, but it doesn't work when the filenames have spaces.
for f in *; do mv "$f" $(basename "$f" .temp) ; done
If I double escape "'$f'" then basename won't read the extension. If I leave it as is then it will think that the second word in the title is the directory I want to move to.
How can I just remove the .temp?

Escaping brackets in filename. Shell script [duplicate]

This question already has answers here:
is it safe to use "ls" in for loop in bash
(3 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have many files with pattern name:
N.apple(d).log
e.g. 001.apple(d).log, 002.apple(d).log, etc.
The problem is when I am trying to do something with the files in a bash script, there is always an error 'No such file or directory' even with escaped brackets:
for i in $(ls my_folder); do file=$(echo $i | sed 's/(/\\(/g' | sed 's/)/\\)/g') ; head -1 my_folder/$file; done
Thanks for any tips
Use quotes. Also, don't use ls when the shell can expand the wildcards just fine already.
for i in my_folder/*; do head -1 "$i"; done

Bash delete files from path that contain space [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 5 years ago.
#!/bin/bash
files_path="/Volumes/HDD/Bogdan Data/"
rm -r "$files_path/files/*"
I'm getting an error that path cannot be found, probably due to the space character in the folder name. How should I approach this?
$files_path needs to be quoted, but the * glob must not be.
rm -r "$files_path"/files/*

Bash script doesn't evaluate variable in filename [duplicate]

This question already has answers here:
What is the difference between ${var}, "$var", and "${var}" in the Bash shell?
(7 answers)
Closed 5 years ago.
I have a bash script which creates a backup of my folder. It should name the tar gz file using a version number but it doesn't:
#!/bin/bash
ver='1.2.3'
tar czf $ver_myfolder.tar.gz .
Expected output:
1.2.3_myfolder.tar.gz
Actual output:
_myfolder.tar.gz
If I append the variable like this:
#!/bin/bash
ver='1.2.3'
tar czf myfolder-$ver.tar.gz .
It works
You should use ${var} here since you are appending underscore after it which is considered a valid character for variable names. Due to that fact shell doesn't know whether you're expanding variable name $ver or $ver_myfolder:
ver='1.2.3'
tar czf "${ver}_myfolder.tar.gz" .
Since $ver_myfolder is unset, you get an empty value.
Because the underscore is a valid character for a variable name, you should use braces to explicitly specify the range of your variable:
${ver}_myfolder.tar.gz
^ ^
Without braces, Bash will actually try to parse
${ver_myfolder}.tar.gz
For your edited question, it is because the dot is not a valid character for a variable name, so Bash will not attempt to parse the dot into the name lookup. Even if you put it into braces, a variable name containing a dot is still invalid:
$ echo ${ver.}
bash: ${ver.}: bad substitution
$ ver.=1.2.3
ver.=1.2.3: command not found

Resources