I use Ubuntu 16.04 (xenial) with Bash 4.3.48(1).

Based on the answer of Stephen Kitt in this session, I tried run the following command as part of a function named imb():

mysqldump -u root -p --all-databases | zip "$drt/db-$date.zip" -

I sourced the file in which the imb() function sits:

source ~/functions.sh

The function

imb() {
    date="$(date +\%F-\%T)"
    mysqldump -u root -p --all-databases | zip "$drt/db-$date.zip" - # Note the hyphen before this comment;
    zip -r "all_zipped-$date.zip" "$drt"/ -x "*/cache/*"
    rm -f "$drt/db-$date.zip"

My problem

Calling the function fails with the zip error:

zip error: Nothing to do! (/var/www/html/db-2018-04-13-22:27:47.zip)

This seems to happen because the hyphen ignored somehow; I assume that from a look in:

type -a imb

which outputs:

imb is a function

imb () {
    date="$(date +\%F-\%T)";
    mysqldump -u root -p --all-databases | zip "$drt/db-$date.zip";
    zip -r "all_zipped-$date.zip" "$drt"/ -x "*/cache/*";
    rm -f "$drt/db-$date.zip" 

I noted the missing hyphen here.

My question

Why does the function fails / why does Bash allegedly ignores the hyphen?

  • Tested imb on bash v4.4-ubuntu1, and the hyphen doesn't vanish from the output of type -a imb. – agc Apr 13 '18 at 23:04
  • 2
    Not related to the problem but the backslashes in date +\%F-\%T are useless. – Hauke Laging Apr 13 '18 at 23:50
  • 3
    Having IFS=- will not make the dash disappear. I believe you may have an old version of the function in your environment. Do unset -f imb and source the file again. – Kusalananda Apr 14 '18 at 7:25
  • 1
    \% has always been useless because % is not a special char for the shell. – Hauke Laging Apr 14 '18 at 14:22
  • 1
    @user9303970 The one place you need \% is in a crontab. % is not a special character in bash, but it is one for cron. – Gilles Apr 14 '18 at 20:43

You had an old version of the function in your environment, and hadn't re-sourced the file after you edited it to add the missing -.

When you did re-source the file, or restart your shell, you obtained the corrected version of the function that you saw in the file. That version works.

That's really all there is to it; there's nothing interesting to explain here. It's a fairly common situation that the environment ends up out of sync with the files that set it up and it leads to momentary confusion often, but it's easily resolved as it was in this case.

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.