The following script does the job of archiving /home/jerzy/testdir/:


XZ_OPT=-9 tar -cJf /home/jerzy/testdir.tar.xz -C /home/jerzy testdir && \   

echo "testdir/ already archived" 

it nevertheless gives me the following output:

line 3:  : command not found
testdir/ already archived

When XZ_OPT=-9 tar -cJf /home/jerzy/testdir.tar.xz -C /home/jerzy testdir is executed from the terminal it does its job without spitting out command not found. Why is it so that when the very same line is executed from the script I get command not found? Specifying the full path, i.e. /usr/bin/tar does not solve the matter.

My environment: Ubuntu Desktop 20.04LTS, which tar returns /usr/bin/tar.

  • What is the purpose of the && followed by \, some spaces, a blank line and a misleading message? Jan 13, 2022 at 2:20
  • @JeremyBoden The purpose of the message is to notify the user that tar succeded in its task. I find it easier to read code when there are 1-2 blank lines in between the lines. Methinks there is no need for `` in this code snippet - thanks to the answer of @they . Such usage stems from my bad education.
    – John Smith
    Jan 13, 2022 at 6:58
  • 1
    echo "testdir/ already archived" - looks like an error message. Surely echo "testdir/ successfully archived" would be better? Jan 13, 2022 at 16:18
  • @JeremyBoden My bad. It is misleading, indeed.
    – John Smith
    Jan 14, 2022 at 17:34

1 Answer 1


After the tar command, you have && and a line continuation. Or rather, it would have been a line continuation if there was no space after the \.

Since you have one or more spaces after the \ at the end of the line, the shell will interpret this as a command whose name is a single space. This command can not be found.

Testing to run a command whose name is a single space:

$ ' '
bash:  : command not found
$ \  # an escaped space
bash:  : command not found

(The name of the command not being found is the second of the two spaces between bash: and : command not found.)

The solution is to remove the spaces after \ so that the backslash is the last character on the line (escaping the newline).

Alternatively, remove the \ altogether. There is no need to have a line continuation at this point in the script.


XZ_OPT=-9 tar -cJ -f ~jerzy/testdir.tar.xz -C ~jerzy testdir &&
echo '"testdir" archived'

I'm using /bin/sh here rather than bash as nothing in the script requires bash. I also took the liberty of using the tilde syntax for accessing the home directory of the user jerzy. This way, your script would work even on systems where home directories are located elsewhere, as they are on, e.g., macOS. If you meant to refer to the home directory of the current user, I would have used "$HOME" instead.

There are a few ways to make it more obvious that the echo command is depending on the success or failure of the tar command. For example, you may indent the echo command:

XZ_OPT=-9 tar -cJ -f ~jerzy/testdir.tar.xz -C ~jerzy testdir &&
    echo '"testdir" archived'

... or, if it does not result in a too long line, simply do not break the line:

XZ_OPT=-9 tar -cJ -f ~jerzy/testdir.tar.xz -C ~jerzy testdir && echo '"testdir" archived'

You could even turn it into a full-blown if-statement:

if XZ_OPT=-9 tar -cJ -f ~jerzy/testdir.tar.xz -C ~jerzy testdir
    echo '"testdir" archived'
  • Your if based one would not propagate tar's failing exit status. You'd need some else exit 1 or else exit Jan 13, 2022 at 11:29
  • @StéphaneChazelas Wouldn't else false be better, at least if we consider this as a replacement for only the given AND-list in a generic script?
    – Kusalananda
    Jan 13, 2022 at 11:47
  • Depends what we want. But it seems to me that if that tar fails the script should probably abort. So maybe even tar ... || exit; echo succeeded; ... would be better if there's more the script is doing afterwards. Jan 13, 2022 at 12:05
  • @StéphaneChazelas The reader has options now, and extra background given by your comments. Thanks for the input (and for that extra edit just now)!
    – Kusalananda
    Jan 13, 2022 at 12:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .