Bash Shell: Cannot Use Variable $ as a Path to Run Tar

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:


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":


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.


Bash - Concatenating Variable on to Path

Tilde expansion doesn't work when inside a variable. You can use the $HOME variable instead:

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

Leave a reply