0

when I use simple bash command

#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"

and run it using source filename.sh it gives me an error :Undefined variable

what could be the problem? I search but there is no solution.

  • Does it run as expected when you invoke ./filename.sh without source? – Kamil Maciorowski Apr 20 '16 at 6:07
  • It gives me "Permission denied" – Loura Apr 20 '16 at 6:13
  • Make the file executable by chmod a+x ./filename.sh, then run ./filename.sh and post the result. – Kamil Maciorowski Apr 20 '16 at 6:14
  • I did it, but it still not working : ./filename.sh: command not found – Loura Apr 20 '16 at 6:17
  • What shell do you use? (try echo $SHELL). What is the output of /bin/bash -c ./filename.sh? – Kamil Maciorowski Apr 20 '16 at 6:21
4

Summary for future reference:

The shebang points to /bin/bash but the source command makes the code run in a current shell which turned out to be csh.

The code works on my bash on Debian and it doesn't work on my csh there, when invoked with source filename.sh. Yet it looks like the OP's intention was to run it with bash.

To make it run with bash as intended, it needs to be made executable (chmod a+x filename.sh) and invoked by ./filename.sh.

And finally the #!/bin/bash shebang had a wrong path in this particular case.

  • Note that the code would work in csh but do something different. Since csh has no read builtin, It would invoke a read command on the filesystem (which could not possibly set csh's $NAME variable), The echo "Hi $NAME!" would work if $NAME was set as an environment variable on in ~/.cshrc or anywhere else in the csh script prior to the source. – Stéphane Chazelas Apr 27 '16 at 14:09

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.