Starting script without ./ name or sh name

Hi, i want to start my script only by the name of it.

$ scriptName

normaly i have to use ./scriptName oder sh scriptName

is there a way to do that in bash or sh ?

Hi,

Try this:

  1. Type a command called as "pwd" on your terminal(where you have written your program), copy the result.

  2. Now type the command "setenv PATH (result of pwd command, excluding the() braces):{$PATH}

This should work.

eg:1. pwd
/home/vishwa/programs

  1. setenv PATH /home/vishwa/programs:{$PATH}

well the setenv command is not found but thats not the problem i think,

you want to add the path of the script to the path of the system variables so i can reach the script in any directory, but thats not the problem i have

i have the script in the same directory where i want to open it, i wanted to know if there is a way without changing some system variables to open it by name so i can call it on an other host where i have copyed it too.

is there a way ?

You place it in the /usr/bin folder, I think.

Edit: Ok, not quite.. I think that just allows you to type scriptName.sh in any directory

Edit again: Aha, got it. Add #!/bin/bash to the top of your script file, remove the .sh extension, and then put it in your /usr/bin folder. You will need to use sudo mv to move it, and sudo chmod +x to execute it.

$ scriptName will then execute /usr/bin/scriptName, interpreting the commands with the bash library.

I already did that but
$ scriptName
bash : criptName : command not found

$cat Script

#!/usr/bin/bash

echo "test"

ls -l scriptName

-rwx-r-xr-x

with ./scriptName it works
or sh scriptName

OS : Debian Lenny
Alternativ : cygwin

on both the same "problem"

In OS X:

brian-1000h:~ admin$ sudo nano /etc/bin/test1
Password:

-----Nano-----
#!/bin/bash
echo "omgwtfhax"

Ctrl+x , y, Enter

brian-1000h:~ admin$ sudo chmod +x /usr/bin/test1
brian-1000h:~ admin$ test1
omgwtfhax

Dunno what the difference is there, other than I have #!/bin/bash at the top of mine, and you have #!/usr/bin/bash.. maybe it's not reading the library correctly because of the /usr/ bit. That would explain why you have to tell it that it's a bash file.

The setenv command is a csh thing.

To add a directory to your PATH in bash:
export PATH=${PATH}:/path/to/directory

Add this to your local profile or /etc/profile. You could also copy the script to any directory already defined in your current path:

echo $PATH

tryed /bin/bash also but didnt work

with that it works, but i want to copy it on an other host and execute it without changing anything there, only with the scriptName.

To do what you want, the script has to be in a directory that is in the $PATH variable.

There is no other way.

PATH=.:$PATH

you can run all executable files in your current folder without giving ./ or sh.

but if a file in the current directory has the same name as another command , the script in your directory will override the other program.

e.g.
echo "clear" > ls
chmod u+x ./ls

now ls will act as clear... so it is kinda dangerous...

so remember to use which commandname if having doubts

If you are going to put the current directory into your PATH (which is not recommended), don't put it first. Put it at the end:

PATH=$PATH:.

It is far better (i.e., safer) to create a directory for your scripts (e.g. $HOME/bin) and add that to your PATH.

ok thx

nice tip cfajohnson, thanks...

usually the default profile scripts on linux loook for the directory $HOME/bin and if it exists automatically add to the path...
so in case you have any of your own programs just put them in ~/bin and you can run them from anywhere..

I created a new directory /local/bin and added it to the PATH and I put all my scripts in that directory so they are easy to find.

EDIT: I see cfajohnson had a similar response.. :stuck_out_tongue: