LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash shell script (https://www.linuxquestions.org/questions/linux-newbie-8/bash-shell-script-151537/)

naka0naka 02-28-2004 09:09 AM

bash shell script
 
I was playing with the following script and it did not produce the expected result.

#! /bin/bash
cd /
echo HELLO


I saw HELLO printed on the screen but my current working directory was not at the root directory. Any idea what went wrong?

ranger_nemo 02-28-2004 09:22 AM

The script doesn't run exactly as if you typed in the commands yourself... When it's done, it's returning you to where you were. You can check this by sticking a new line in the script before and after the cd...

echo $PWD

This will print out the current directory the script is in. You will see that it starts in your current directory, then moves to root.

naka0naka 02-28-2004 09:43 AM

Thanks. I got it.

Is there anyway i can make the change permanent?

ranger_nemo 03-02-2004 12:19 PM

Maybe, but I don't know it.

markjuggles 03-02-2004 12:26 PM

Here are two ideas:

1. "Dot Space" your command: $ . my_script

This will run the script in the current shell. By default your scripts run in a new shell which gets destroyed when it's done. That is why your environment is unchanged.

2. Write an alias for your utility rather than a script. I haven't done this with bash but it works for ksh. Look for 'alias' in the bash man page.

Mark

naka0naka 03-03-2004 12:22 PM

thanks

jim mcnamara 05-28-2004 01:10 PM

The unix source command also does what the dot command does.

Code:

source ./myscript
# is the same as
. ./myscript


jlliagre 05-28-2004 03:06 PM

For more than one line scripts, shell functions supersedes the alias command.


All times are GMT -5. The time now is 11:20 PM.