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.
Related videos on Youtube
Author by
Loura
Updated on September 18, 2022Comments
-
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 about 8 yearsDoes it run as expected when you invoke
./filename.sh
withoutsource
? -
Loura about 8 yearsIt gives me "Permission denied"
-
Kamil Maciorowski about 8 yearsMake the file executable by
chmod a+x ./filename.sh
, then run./filename.sh
and post the result. -
Loura about 8 yearsI did it, but it still not working : ./filename.sh: command not found
-
Kamil Maciorowski about 8 yearsWhat shell do you use? (try
echo $SHELL
). What is the output of/bin/bash -c ./filename.sh
? -
Loura about 8 yearsI am working on Freebsd shell. and this is my first time using it. the output is bin/csh
-
Rahul about 8 yearsDo you have bash installed? If not use FreeBSD Ports to install it. Use
where bash
to find out. -
Loura about 8 yearsThank you so much. when I try
where bash
, it gives meusr/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 about 8 yearsFor the record: it works fine with
source filename.sh
or./filename.sh
only? -
Rahul about 8 years@Loura : please accept the answer, if you do not know how to accept it read this.
-
-
Stéphane Chazelas about 8 yearsNote that the code would work in
csh
but do something different. Sincecsh
has noread
builtin, It would invoke aread
command on the filesystem (which could not possibly setcsh
's$NAME
variable), Theecho "Hi $NAME!"
would work if $NAME was set as an environment variable on in~/.cshrc
or anywhere else in thecsh
script prior to thesource
.