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? |
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. |
Thanks. I got it.
Is there anyway i can make the change permanent? |
Maybe, but I don't know it.
|
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 |
thanks
|
The unix source command also does what the dot command does.
Code:
source ./myscript |
For more than one line scripts, shell functions supersedes the alias command.
|
All times are GMT -5. The time now is 11:20 PM. |