Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a shell file, for example wd.sh and I want to turn this into a command called wd that I can run without having to sh it, just like running cat, sort, or any other UNIX command.

How do I do this?

Like

wd file1 file2

share|improve this question

migrated from stackoverflow.com Mar 18 '13 at 3:23

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers

I recommend this way (using a user local ~/bin dir) :

cd
mkdir -p bin
cat<<EOF>bin/wd
#!/bin/bash

echo "it works"
EOF
chmod +x bin/wd
export PATH=~/bin:$PATH
wd

(You just have to cppy/paste the whole code in the same time in a (pseudo)terminal)

To keep the PATH modified after opening another shell, use an editor to open ~/.bashrc and add the

export PATH=~/bin:$PATH

line.

share|improve this answer
    
Fixed typo in post –  sputnick Mar 17 '13 at 20:15
2  
For security's sake, it's probably best to have your personal ~/bin folder at the end of your path, rather than the beginning: export PATH="$PATH:~/bin". This way if some piece of malware sneaks into it, it won't get run before an identically-named system command, for example. –  MattDMo Mar 18 '13 at 3:33
add comment

Simpler solution: make an alias !

echo "alias wd='bash /somedir/somesubdir/myscript.sh'" >> ~/.bash_aliases
source ~/.bashrc

And you're done. You don't even need to make the script executable

share|improve this answer
    
Not everyone have source ~/.bash_aliases in ~/.bashrc –  sputnick Mar 18 '13 at 10:04
    
Shell functions are preferred over aliases. –  chepner Mar 18 '13 at 17:48
    
@chepner: preferred by you ? by yourself ? Using a function is useless in this case. –  doukremt Apr 10 '13 at 17:02
    
From the bash man page: "For almost every purpose, aliases are superseded by shell functions." –  chepner Apr 10 '13 at 18:55
add comment

Simply add the path of the current folder to the ~/.bashrc

export PATH=$PATH:CURRENT_DIRECTORY_PATH
share|improve this answer
add comment

Your Answer

 
discard

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