Reading user input with read back

6,215

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.

Share:
6,215

Related videos on Youtube

Loura
Author by

Loura

Updated on September 18, 2022

Comments

  • Loura
    Loura over 1 year

    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.

    • Kamil Maciorowski
      Kamil Maciorowski about 8 years
      Does it run as expected when you invoke ./filename.sh without source?
    • Loura
      Loura about 8 years
      It gives me "Permission denied"
    • Kamil Maciorowski
      Kamil Maciorowski about 8 years
      Make the file executable by chmod a+x ./filename.sh, then run ./filename.sh and post the result.
    • Loura
      Loura about 8 years
      I did it, but it still not working : ./filename.sh: command not found
    • Kamil Maciorowski
      Kamil Maciorowski about 8 years
      What shell do you use? (try echo $SHELL). What is the output of /bin/bash -c ./filename.sh?
    • Loura
      Loura about 8 years
      I am working on Freebsd shell. and this is my first time using it. the output is bin/csh
    • Rahul
      Rahul about 8 years
      Do you have bash installed? If not use FreeBSD Ports to install it. Use where bash to find out.
    • Loura
      Loura about 8 years
      Thank you so much. when I try where bash , it gives me usr/local/bin/bash . I change the !/bin/bash to !/user/local/bin/bash and it works fine. I can't thank all enough.
    • Kamil Maciorowski
      Kamil Maciorowski about 8 years
      For the record: it works fine with source filename.sh or ./filename.sh only?
    • Rahul
      Rahul about 8 years
      @Loura : please accept the answer, if you do not know how to accept it read this.
  • Stéphane Chazelas
    Stéphane Chazelas about 8 years
    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.