Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I typically use cd Directory/Directory/ to point to the shell directory and then type bash shell.sh, but I was wondering if there is an easier way to execute my shell & the location with one line? I do bounce to different folders often.

Example: unix folder with a shell script named alpha

cd Desktop/unix
bash alpha.sh
share|improve this question
add comment

3 Answers

You can Set PATH in your system Environment eg.

export PATH=$PATH:/absolute/path/Desktop/unix

also you can put above line in ~/bashrc file, then you don't need to set that path again

Note:- Your script should be executable in that location then you can run your script from any directory.

Refer this page for more information

share|improve this answer
    
I am familar with that method but that still doesn't solve if there is a folder within the defined path. So after setting the path you would still have to do cd folder and run bash shell.sh. I'm looking for a solution that can bounce to different directories AND execute the code within one line. –  Gramps Jan 3 '13 at 17:30
    
then set this PATH=$PATH$( find $HOME/scripts/ -type d -printf ":%p" ) –  Rahul Patil Jan 3 '13 at 17:46
    
refer this link for PATH with sub-directories stackoverflow.com/questions/657108/… –  Rahul Patil Jan 3 '13 at 17:47
    
Another option is to symlink the contents of the subdirs, and place the link in the top-level dir. –  ire_and_curses Jan 3 '13 at 22:15
add comment

If you set a script file executable:

chmod o+x alpha.sh

And include the appropriate shebang (#!/bin/bash) at the top, you do not have to invoke it with bash. See man chmod for an explanation of o+x; you may prefer a+x.

As for the path, you can add that as per Rahul's suggestion, or you can do something similar without adding it to your $PATH, eg:

export SCRIPTS=~/Desktop/unix

Now anything executable in there can be used:

$SCRIPTS/alpha.sh
share|improve this answer
add comment

A answer with a different perspective, may help in better understanding of *nix environment.

All the commands/utilities you know of in *nux are present in some or the other directory which you can find using the whereis, or which command

Example

$ which ls
/bin/ls
$ which sort
/usr/bin/sort

So, how these get located which you try to execute the command?

The answer is the PATH variable. If you do a echo $PATH, you'll see a colon : separated list of directories.

Sample output for $PATH

/home/mtk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/1.9.1/bin/:/usr/local/ant/apache-ant-1.8.3/bin

so, if you place your own executable at one of those locations, you'll be able to run it just as other system commands. Or, you can add your own defined directory to this list, by appending it to the PATH variable, and exporting it, so that it is available for the complete shell, like

export PATH=$PATH:/home/mtk/myUtilities/bin

You may want to add this particular line to .bashrc file, so that you don't need to execute the above command everytime you start the shell.

Just for fun

Ty changing the PATH variable, make it empty, you'll end up with useless shell, until you reset the PATH properly again.

$ export PATH=""
$ ls
bash: ls: No such file or directory
$ which pwd
bash: which: No such file or directory
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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