I'm running the following script at home that I found online and every time I do, the shell terminates when I supply an answer to the read prompt. When I run the script in the bg, I get the following message: Bash: [answer]: command not found. Why is this happening? I usually run it from the directory the script is kept using .

Edit: I ran the script with bash instead, and the script worked as expected. Can anyone explain why the difference?


echo "Enter 1 or 2, to set the environmental variable EVAR to Yes or No"
read ans

# Set up a return code

if [ $ans -eq 1 ]  
    export EVAR="Yes"
    if [ $ans -eq 2 ]
    export EVAR="No"
# can only reach here with a bad answer
    export EVAR="Unknown"
echo "The value of EVAR is: $EVAR"
exit $RC

closed as unclear what you're asking by Rui F Ribeiro, ilkkachu, Jeff Schaller Sep 13 at 19:30

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • How did you execute your script? Paste your execution history and results – NeilWang Sep 12 at 4:33
  • 10
    Do you still tend to source scripts? – Kamil Maciorowski Sep 12 at 4:34
  • How do you run it? Post a sample run that shows exactly what happens. What you write to the command line, what is the output. – ilkkachu Sep 12 at 17:39

It's pretty much the same answer as for your otherwise-identical question on SuperUser.

When you run a shell script with . (or source) you are running it in the context of your interactive shell. Thus, when the shell hits an exit command it exits your interactive shell.

The correct way to run a shell script is not to use . (or source) but to make it executable and just run it as if it were any other application. (The first line, starting with #!, defines the interpreter for the script. In my example it's /bin/bash but it could be anything that can read a script.)

Here is a worked example for a script that's called tryit:

# Create a trivial shell script called "tryit"
cat >tryit <<'X'
echo "This is my shell script"
exit 0

# Make it executable
chmod +x tryit

# Now run it (the "./" prefix means "in the current directory")

If you put the tryit script into a directory that is in your $PATH you don't need (and must not use) the ./ prefix. Here is an example

# Create applications (bin) directory and add to PATH for this session
mkdir -p "$HOME/bin"
export PATH="$HOME/bin:$PATH"

# Move the "tryit" application to the bin directory
mv tryit "$HOME/bin"

# Now we can run it just like any other application

I should probably point out that when running a script this way it is not possible to set environment variables in the current interactive shell. To do that you really do need to continue using . or source, in which case you must remember that because the script runs in your interactive context you must not include an exit statement anywhere.

Not the answer you're looking for? Browse other questions tagged or ask your own question.