0

I have a bash script called a located in /home/agrillet/ where I do this command:

BOOTSTRAP_MODIF="stat -c %Y '/home/agrillet/a'"

The output is: ./a: line 60: stat -c %Y '/home/agrillet/a': No such file or directory

But when I run the same command in my terminal, it works:

$ stat -c %Y '/home/agrillet/a'
1517831544

I am not running the command as root in both cases, can someone explain what is happening and how to be able to get information about the script while running it?

If I use another command in the script such as cat /home/agrillet/a, I am able to print the script from the script itself.

  • 2
    if that's supposed to be command substitution, you need $() around the command. e.g. BOOTSTRAP_MODIF="$(stat -c %Y '/home/agrillet/a')". BTW, did you edit that line before posting it - in particular, is there a space character after the =? that's the most likely explanation for the error message you posted. – cas Feb 5 '18 at 12:08
4

This:

BOOTSTRAP_MODIF="stat -c %Y '/home/agrillet/a'"

Sets the variable BOOTSTRAP_MODIF to the string stat -c %Y '/home/agrillet/a'. It doesn't produce any output.

If you expand the variable later, as "$BOOTSTRAP_MODIF" (with quotes), you get exactly that string, as one string. The error message looks like Bash trying to execute a command of that name (spaces and all), which you don't have.

You can run the script with bash -x (or sh -x), or use set -x inside the script to see the commands the shell actually tries to run.

See BashFAQ 050 on how to save a command for later use.

  • Thank you, a simple mistake that had me confused due to the message reported. Looking at the error message again, the error line was an important information that I missed: the issue was not while instantiating BOOTSTRAP_MODIF but when using it. – Armand Grillet Feb 5 '18 at 12:56

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.