Bash Shell: Cannot use variable $ as a path to run tar
~
doesn't expand to your home directory in double quotes.
Just remove the double quotes:
backup_source=~/momobobo
backup_dest=~/momobobo_backup/
In cases where you have things you would want to quote, you can use ~/"momobobo"
Bash Script Help - Tar not working with variables?
By overriding the shell's built-in PATH
variable, you are causing it to not find the tar
command. Use another variable name, and generally, refrain from using uppercase variable names.
Problem getting bash to pass variable with spaces as single filename to tar
I'd use mapfile
to read backup_dirs.txt
into a bash-array:
mapfile -t BACKUP_DIRS <backup_dirs.txt
and then pass it to tar
like this:
tar -cjvPf backup.tar.bz2 "${BACKUP_DIRS[@]}"
If you have lots of backup directories I'd recommend using command-line option -T
to specify a text file directly:
tar -cjvPf backup.tar.bz2 -T backup_dirs.txt
Bash: Why can't I assign an absolute path to a variable?
Remove the space before "/home/foobar"
:
#!/bin/bash
DIR="/home/foobar"
echo "$DIR/test"
Error: Tar command not found
You're saving something into PATH
which is what the shell will search to find the executables. So when you use that variable the shell can't find, say, tar
because it is no longer in your search path. Use a different variable name.
To create a tar file inside a script
probably your $BASEFOLDER variable is empty might be one of the others though.
put these 2 lines just before the tar command and try it.
echo -e "PATH = $UNIXPATH\nTAR = $TAR_NAME\nBASE = $BASEFOLDER"
exit
Bash - Concatenating Variable on to Path
Tilde expansion doesn't work when inside a variable. You can use the $HOME
variable instead:
#!/bin/bash
p=$HOME
tar xvf "$p/some_file.tar"
Trouble with debugging tar in bash script
It is a reasonable treatment to enclose each filename with single quotes
considering filenames containing whitespaces.
The problem is when passing the variable "$paths" to tar
as an argument,
the single quotes are literally interpreted as a part of the filename.
Please try:
echo "$paths" | xargs tar czf output.tgz; fatal_check
in place of:
tar czf output.tgz $paths; fatal_check
Note that we need to assume the original filenames do not contain the single quotes.
Hope this helps.
Related Topics
R Programming - Submitting Jobs on a Multiple Node Linux Cluster Using Pbs
Linux Kernel: Copy_From_User - Struct with Pointers
Bash: How to Perform Arithmetic on Numbers in a Pipe
How to Get Around the Linux "Too Many Arguments" Limit
Dotnetcore: Cross Platform Version of Getinvalidfilenamechars
Replace Line with Space and Backslash with a String Containing Spaces
One-Liner: Print All Lines Except the Last 3
Linux Perf Record: Difference Between Count (-C) and Frequency (-F) Options
Compile Git for 32-Bit Linux on Shared Hosting
Why Do I Get /Etc/Cups Conflicts Between Attempted Installs in Yocto
Asp Net Core Linux Err_Connection_Refused
Knowing If a Remote Port Forward Was Successful
Export_Symbol in Kernel Module | Undefined Symbol During Insmod