LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting (https://www.linuxquestions.org/questions/programming-9/bash-scripting-73889/)

kbeaver 07-18-2003 12:57 PM

Bash scripting
 
Got a quick question:
I am trying to run scripts in Red Hat 9.0, I include the #!/bin/bash directive, so I try to run the script my typing its name, nothing. If I type "bash script_name" it will run fine. Any way to get around the bash command?

david_ross 07-18-2003 01:06 PM

What error do you get?
What are the permissions on the file? Make sure it is executable by the user you are running it as.

Is it in your path? If not then cd to the dir it is in and run:
./script_name

kbeaver 07-18-2003 01:35 PM

I do not get any error, it just hard returns back to by prompt. I set the file permission to 777, and I am in the correct directory. The ./ worked...I thought there was a way to make it work by just typing the file name...am I wrong?

david_ross 07-18-2003 01:41 PM

Yes you need to put it somewhere in your path. eg:
/bin

So the script would be:
/bin/your_script

then running:
your_script will work.

Hko 07-18-2003 06:58 PM

To be more precise, you either need to specify the directory where the script is (e.g. ./your_script, or /home/yourname/your_script, etc.) or put it in a directory listed in the PATH environment variable (seperated by colons ':'). Normally the directories /usr/local/bin:/usr/bin:/bin are in the PATH var. This is arranged by your distribution. To see the directories where the shell will find the script by itself through the PATH var, type:

echo $PATH

If you put your script in one of the directories you see, you can just type its name (without ./ ).

You can also include a directory of your choice to the PATH var, say the directory "myscripts" in your home directory. Make sure this dir exists, put you script there, and type:

export PATH=${PATH}:${HOME}/myscripts

By convention a directory named "bin" is used for this. Some distributions arrange for the bin directory as a subdir of your homedir to be included automatically. This is done by some lines in your ~/.bashrc or ~/.bash_profile or maybe ~/.profile. These lines resemble somthing like:

# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
PATH=~/bin:"${PATH}"
fi

To have you custom dir "/home/yourname/myscripts" permanently in the PATH var. Add the following line at the end of your ~/.bashrc or ~/bash_profile:

export PATH=${PATH}:${HOME}/myscripts

Hope this helps.

gabriele_101 07-18-2003 08:35 PM

FYI: You can also put "." in your PATH and then the current directory is searched, much like in DOS. Generally, however, this is not recommended, though I personally prefer it for my non-root development user.

e.g. put in your ~/.bashrc:

PATH=$PATH:.

Or something of the sort. If you do put "." in your PATH, you should make it the last thing in the PATH.

-G


All times are GMT -5. The time now is 10:48 AM.